1

I am new to angular js . I have a json response which i want to convert to list of objects and use ng-repeat to display these object. I am getting a list of these objects which are as follow

    1 : 
{"_id":"56e75b42c9cbbb7765414cdf",
"email":"email@email.com",
"phone":"02-653-2900",
"display_name":"KFC",
"registered_name":"KFC",
"__v":0,
"address":
    {"country":"TH",
       "state":"TH14,
       "postcode":"10150",
       "suburb":"Klongteay",
       "address2":"Sukhumvit Rd",
       "address1":"142 Two Pacific Place"},
"date_modified":"2016-03-15T00:45:54.631Z",
"date_created":"2016-03-15T00:45:54.626Z",
"status":"active",
"logo":{"path_to_file":"90cf845d95.png"}
}

I have saved the above object in $scope.results and i want to do something like

<div ng-repeat="user in  results.data  ">
           <span>Phone </span><span>{{user.phone}}:</span> 
            <span>Display Name</span><span>{{user.display_name}}:</span> 
</div>

How can i achieve this?

Suman Palikhe
  • 357
  • 1
  • 4
  • 16
  • Apart from AngularJS, you can use a simple `JSON.parse`. See http://stackoverflow.com/questions/4935632/parse-json-in-javascript and https://msdn.microsoft.com/library/cc836466(v=vs.94).aspx – lealceldeiro Mar 06 '17 at 13:00
  • Take care you missed a double quote at last line in the state value `"address": {"country":"TH", "state":"TH14",` apart from that it should work as it is. – nullbyte Mar 06 '17 at 13:03
  • post your js code also, which one you are using in your `get`, `.then` or `.success` ? – Pratyush Pranjal Mar 06 '17 at 13:04

2 Answers2

0

Create a new controller

angular.module('appname').controller('ctrl', function($scope, $http){
 $http.get('url-to-json').then(function(res){
  $scope.results = res;
 });
})
Gaurav Kumar Singh
  • 1,550
  • 3
  • 11
  • 31
0

Nothing is wrong with your ng-repeat but the JSON is not valid. A small double quote is missing in "state":"TH14,

Here is the working plunker http://plnkr.co/edit/Z4n9oSIznVnFsNTOcLJX?p=preview

It should be

    1 : 
{"_id":"56e75b42c9cbbb7765414cdf",
"email":"email@email.com",
"phone":"02-653-2900",
"display_name":"KFC",
"registered_name":"KFC",
"__v":0,
"address":
    {"country":"TH",
       "state":"TH14",
       "postcode":"10150",
       "suburb":"Klongteay",
       "address2":"Sukhumvit Rd",
       "address1":"142 Two Pacific Place"},
"date_modified":"2016-03-15T00:45:54.631Z",
"date_created":"2016-03-15T00:45:54.626Z",
"status":"active",
"logo":{"path_to_file":"90cf845d95.png"}
}
Anand G
  • 3,130
  • 1
  • 22
  • 28