Reputation: 179
This is the json the format i received and i want to print the msg and the username field..How to do that? and also want to format the date in Y-m-d format...how i will acheive it..i have tried ng- repeat but i couldnt print the data ..so please help me to print this json array in angualar js html
HTML
<ul>
<li>msg</li>
<li>date</li>
</ul>
{
"messages": [
{
"_id": "sdfsdfsd",
"t": "subscription-role-removed",
"rid": "sdfsfsfd",
"ts": "2017-05-10T07:03:26.865Z",
"msg": "samim",
"u": {
"_id": "sdfdsf",
"username": "dsdfsfsd"
},
"groupable": false,
"role": "moderator",
"_updatedAt": "2017-05-10T07:03:26.865Z"
},
{
"_id": "sdfsdfsdfssd",
"t": "subscription-role-added",
"rid": "asdafdaf",
"ts": "2017-05-10T06:51:23.147Z",
"msg": "samim",
"u": {
"_id": "ssdfsdf",
"username": "adfffffsfmin"
},
"groupable": false,
"role": "moderator",
"_updatedAt": "2017-05-10T06:51:23.147Z"
},
{
"_id": "fafasfasf",
"t": "au",
"rid": "sdfsdfsfsdf",
"ts": "2017-05-10T06:50:14.029Z",
"msg": "samim",
"u": {
"_id": "sdfsdfsdfsdf",
"username": "sdfsdfsd"
},
"groupable": false,
"_updatedAt": "2017-05-10T06:50:14.036Z"
},
{
"_id": "sfdsfsdfsd",
"rid": "sfsdfsdfs",
"msg": "hi",
"ts": "2017-05-10T06:44:27.610Z",
"u": {
"_id": "sdfsdfsdf",
"username": "sfsdfsdf"
},
"_updatedAt": "2017-05-10T06:44:27.611Z"
},
{
"_id": "sdfsdfsd",
"rid": "sdfsdfsdfsdf",
"msg": "hello",
"ts": "2017-05-10T06:38:10.509Z",
"u": {
"_id": "sdfsdfsd",
"username": "sdfsdfsdsdf"
},
"_updatedAt": "2017-05-10T06:38:10.514Z"
}
],
"success": true
}
Upvotes: 0
Views: 27
Reputation: 41387
add the ng-repeat1 like this with
date` filter
<ul ng-repeat="item in arr.messages">
<li>
{{item.u.username}}
</li>
<li>
{{item.ts | date : 'yyyy-M-d'}}
</li>
</ul>
angular.module("app",[])
.controller("ctrl",function($scope){
$scope.arr = {
"messages": [
{
"_id": "sdfsdfsd",
"t": "subscription-role-removed",
"rid": "sdfsfsfd",
"ts": "2017-05-10T07:03:26.865Z",
"msg": "samim",
"u": {
"_id": "sdfdsf",
"username": "dsdfsfsd"
},
"groupable": false,
"role": "moderator",
"_updatedAt": "2017-05-10T07:03:26.865Z"
},
{
"_id": "sdfsdfsdfssd",
"t": "subscription-role-added",
"rid": "asdafdaf",
"ts": "2017-05-10T06:51:23.147Z",
"msg": "samim",
"u": {
"_id": "ssdfsdf",
"username": "adfffffsfmin"
},
"groupable": false,
"role": "moderator",
"_updatedAt": "2017-05-10T06:51:23.147Z"
},
{
"_id": "fafasfasf",
"t": "au",
"rid": "sdfsdfsfsdf",
"ts": "2017-05-10T06:50:14.029Z",
"msg": "samim",
"u": {
"_id": "sdfsdfsdfsdf",
"username": "sdfsdfsd"
},
"groupable": false,
"_updatedAt": "2017-05-10T06:50:14.036Z"
},
{
"_id": "sfdsfsdfsd",
"rid": "sfsdfsdfs",
"msg": "hi",
"ts": "2017-05-10T06:44:27.610Z",
"u": {
"_id": "sdfsdfsdf",
"username": "sfsdfsdf"
},
"_updatedAt": "2017-05-10T06:44:27.611Z"
},
{
"_id": "sdfsdfsd",
"rid": "sdfsdfsdfsdf",
"msg": "hello",
"ts": "2017-05-10T06:38:10.509Z",
"u": {
"_id": "sdfsdfsd",
"username": "sdfsdfsdsdf"
},
"_updatedAt": "2017-05-10T06:38:10.514Z"
}
],
"success": true
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<ul ng-repeat="item in arr.messages">
<li>
{{item.u.username}}
</li>
<li>
{{item.ts | date : 'yyyy-M-d h:mm:ss '}}
</li>
</ul>
</div>
Upvotes: 1
Reputation: 38161
ng-repeat
is actually what you want here. and for date formatting, you can use date
filter.
refer below example.
angular.module("app", [])
.controller("myCtrl", function($scope) {
$scope.data = {
"messages": [{
"_id": "sdfsdfsd",
"t": "subscription-role-removed",
"rid": "sdfsfsfd",
"ts": "2017-05-10T07:03:26.865Z",
"msg": "samim",
"u": {
"_id": "sdfdsf",
"username": "dsdfsfsd"
},
"groupable": false,
"role": "moderator",
"_updatedAt": "2017-05-10T07:03:26.865Z"
},
{
"_id": "sdfsdfsdfssd",
"t": "subscription-role-added",
"rid": "asdafdaf",
"ts": "2017-05-10T06:51:23.147Z",
"msg": "samim",
"u": {
"_id": "ssdfsdf",
"username": "adfffffsfmin"
},
"groupable": false,
"role": "moderator",
"_updatedAt": "2017-05-10T06:51:23.147Z"
},
{
"_id": "fafasfasf",
"t": "au",
"rid": "sdfsdfsfsdf",
"ts": "2017-05-10T06:50:14.029Z",
"msg": "samim",
"u": {
"_id": "sdfsdfsdfsdf",
"username": "sdfsdfsd"
},
"groupable": false,
"_updatedAt": "2017-05-10T06:50:14.036Z"
},
{
"_id": "sfdsfsdfsd",
"rid": "sfsdfsdfs",
"msg": "hi",
"ts": "2017-05-10T06:44:27.610Z",
"u": {
"_id": "sdfsdfsdf",
"username": "sfsdfsdf"
},
"_updatedAt": "2017-05-10T06:44:27.611Z"
},
{
"_id": "sdfsdfsd",
"rid": "sdfsdfsdfsdf",
"msg": "hello",
"ts": "2017-05-10T06:38:10.509Z",
"u": {
"_id": "sdfsdfsd",
"username": "sdfsdfsdsdf"
},
"_updatedAt": "2017-05-10T06:38:10.514Z"
}
],
"success": true
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="app" ng-controller="myCtrl">
<ul>
<li ng-repeat="item in data.messages">
<span>{{item.msg}}</span>
<span>{{item.u.username}}</span>
<span>{{item.ts | date: 'yyyy-MM-dd'}}</span>
</li>
<ul>
</div>
Upvotes: 1