简体   繁体   English

angularjs - 用json重复

[英]angularjs - ng repeat with json

how can i use ng-repeat for below json 我怎样才能在json下面使用ng-repeat

$scope.prlists = {
  "1": [{
    "id": "1",
    "name": "One",
    "qty": 2,
    "amount": "1.00",
    "cat": "1.00"
  }],
  "3": [{
    "id": "3",
    "name": "backit",
    "qty": 3,
    "amount": "2.00",
    "cat": "2.00"
  }]
}
<div ng-repeat="pro in prlists">
  name : pro.name
</div>

can not able to get the name due to inner array. 由于内部数组,无法获取名称。 How to solve 怎么解决

ngRepeat can iterate over object properties. ngRepeat可以迭代对象属性。 So, you can do something like 所以,你可以做点什么

<div ng-repeat="(key, pro) in prlists">
    name: {{pro[0].name}}
</div>

See the documentation at: https://docs.angularjs.org/api/ng/directive/ngRepeat 请参阅以下文档: https//docs.angularjs.org/api/ng/directive/ngRepeat

Note that this will not guarantee the order in Angular version >= 1.4, since it will depend on the browser's ordering. 请注意,这不能保证Angular版本中的顺序> = 1.4,因为它取决于浏览器的顺序。 You might be able to sort it using the orderBy filter 您可以使用orderBy过滤器对其进行排序

<div ng-repeat="(key, pro) in prlists | orderBy:key">
    name: {{pro[0].name}}
</div>

See: https://docs.angularjs.org/api/ng/filter/orderBy 请参阅: https//docs.angularjs.org/api/ng/filter/orderBy

If the inner arrays are not just a single element, you may have to nest a second ngRepeat inside the first div. 如果内部数组不仅仅是单个元素,则可能必须在第一个div中嵌套第二个ngRepeat

As I have already commented: 正如我已经评论过:

try pro[0].name 试试pro [0] .name

Reason: 原因:

In you code, for every iteration, $data( pro ) will have 在你的代码中,对于每次迭代,$ data( pro )都会有

[{
  "id": "1",
  "name": "One",
  "qty": 2,
  "amount": "1.00",
  "cat": "1.00"
}]

Now as you see, this is not an object, so you have to go to its first and only child. 现在如你所见,这不是一个对象,所以你必须去找它的第一个也是唯一一个孩子。 Also you are missing {{}} . 你也错过了{{}} name : pro.name this will print pro.name as text and not parse it. name : pro.name这将打印pro.name作为文本而不解析它。

Working demo . 工作演示

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

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