简体   繁体   English

用ng重复显示JSON

[英]Displaying JSON with ng repeat

I have a JSON data in the format 我有一个格式的JSON数据

[

    {
        "_1": {
            "id": 4,            
            "cost": 45.0,
            "measure": 4,
            "NoOfUnits": 677,            
            "hours": null
        },
        "_2": {
            "id": 1,
            "name": "Truck",
            "description": "Test"        
        }
    },
    {
        "_1": {
            "id": 1,
             "cost": 1120.0,
            "measure": 1,
            "NoOfUnits": 500,
             "hours": null
        },
        "_2": {
            "id": 7,
            "name": "PC300",
            "description": null           
        }
    },
]

I'm not able to display the data that I have store in a $scope variable say 我无法显示我存储在$ scope变量中的数据,例如

$scope.result $ scope.result

This is my ng repeat functionality 这是我的重复功能

<div ng-repeat="data in result">{{data.name}}{{data.description}}</div>

your $scope.result is contains 2 objects , with in one object u have set of object properties like _1,_2 , and then these properties are again objects like 您的$scope.result包含2 objects ,在一个对象中,您具有一组object properties例如_1,_2 ,然后这些properties又是对象,例如

"_1": {
    "id": 4,            
    "cost": 45.0,
    "measure": 4,
    "NoOfUnits": 677,            
    "hours": null
}

, then u have the properties u need to print. ,则您具有需要打印的properties

<ul>
  <li ng-repeat="x in result">        // repeats objects
       <ul ng-repeat="obj in x">      // repeat object properties '_1',"_2" , these are again objects
           {{obj.name}}{{obj.description}}
       <ul>
  </li>
</ul>

With your data structure... You cannot access name and description directly from first ng-repeat. 使用您的数据结构...您无法直接从第一个ng-repeat中访问名称和描述。

Even if you have nested ng-repeat you are not guaranteed with name and description. 即使您嵌套了ng-repeat,也无法保证其名称和描述。 You need to flatten the object after first ng-repeat and then you can access all the properties. 您需要先ng-repeat后再展平对象,然后才能访问所有属性。 Assuming that _1, _2 object properties are related. 假设_1,_2对象属性相关。

Two loops are required--- 需要两个循环--

<div ng-repeat="data in result">
   <div ng-repeat="obj in data">
       {{obj.cost}}{{obj.name}}
    </div>
</div>

Demo: http://jsfiddle.net/HB7LU/8254/ 演示: http : //jsfiddle.net/HB7LU/8254/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM