分解多个阵列中的JSON响应

时间:2015-02-06 22:55:41

标签: json angularjs

我试图将这个json响应分解为我可以在前端访问的多个数组。我有一个json响应,例如:

 {
"clients": [
{
  "id": "2",
  "f_name": "test",
  "l_name": "test",
  "email": "test@test.com",
  "company_name": "asd",
  "street": "asd",
  "city": "asd",
  "state": "asd",
  "zip": "54913",
  "identity_id": "6",
  "admin_id": "1",
  "created_at": "2015-01-22 22:55:38",
  "updated_at": "2015-02-04 04:03:13"
},
{
  "id": "3",
  "f_name": "dsf",
  "l_name": "df",
  "email": "sdf@sd.com",
  "company_name": "asdf",
  "street": "sdf",
  "city": "asdf",
  "state": "asdf",
  "zip": "asdf",
  "identity_id": "6",
  "admin_id": "1",
  "created_at": "2015-01-23 17:49:51",
  "updated_at": "2015-01-23 17:49:51"
}
   ],
  "identity": {
"id": "6",
"name": "Test Company",
"street": "13 Street",
"city": "An",
"state": "State",
"zip": "54913",
"admin_id": "1",
"created_at": "2015-01-17 15:34:12",
"updated_at": "2015-02-04 03:43:40",
"image": "https:\/\/s3_whatever",
"phone": "",
"email": "",
"paypal": "whatever_whatever.com"
  }
}

我想将'clients'放入$ scope.clients并将身份放入$ scope.identity

这是我的代码:

 var app = angular.module("myApp", []);

 app.controller("TestController", function ($scope, $http) {


$http.get('http://localhost:8000/clients/json').success(function(clients) {
    $scope.clients = clients.clients;
    $scope.identity = clients.identity; 
});


 });

这是前端代码:

    <div ng-app="myApp">

 <div ng-controller="TestController">
<ul>

 <li ng-repeat="client in clients">
 {{ client.email }}
 </li>

</ul>
 </div>

 <h1>
 {{ identity }}
 </h1>

 </div>


<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.12/angular.min.js">     </script>
<script src='/js/app.js'></script>

1 个答案:

答案 0 :(得分:1)

关闭控制器并使identity超出范围

<div ng-app="myApp">  
    <div ng-controller="TestController">
        <ul>
            <li ng-repeat="client in clients">
                {{ client.email }}
            </li>
        </ul>
    </div><!--END OF TEST CONTROLLER AND $Scope FOR THAT CONTROLLER -->   
    <h1>
 {{ identity }}
 </h1>  
</div>

修复它只需将你的h1移动到控制器div

<div ng-app="myApp"> 
    <div ng-controller="TestController">
        <ul> 
            <li ng-repeat="client in clients">
                {{ client.email }}
            </li>
        </ul>
       <h1>{{ identity }}</h1>
    </div><!-- end of TestController -->
</div><!-- end of myApp -->