Angularjs ng-repeat数组对象数组

时间:2015-09-03 13:08:52

标签: angularjs angularjs-ng-repeat

我正在尝试对这个对象数组使用ng-repeat,但是我无法显示正确的名称 - 值对。

var names = [
[{name: "Name 1", age: 25}, {name: "Name 2", age: 20}],
[{name: "Name 3", age: 50}, {name: "Name 4", age: 40}],
[{name: "Name 5", age: 20}, {name: "Name 6", age: 40}]
];

这可以显示整个对象数组:

<p ng-repeat="name in names"></p>

但是以下显示名称不会:

<p ng-repeat="name in names">
    <p ng-repeat="n in name">
        {{n.name}}
    </p>
</p>

我知道我肯定错过了一些简单的事情。

2 个答案:

答案 0 :(得分:1)

看起来像嵌套的 p 标签会导致ng-repeat混乱开始/结束标记

如果您将内部 p 标记更改为范围 div ,则可以正常运行:

<p ng-repeat="name in names">        
    <span ng-repeat="n in name">
        {{n.name}}
    </span>
</p>

答案 1 :(得分:0)

如果您可以更改多个数组以使用包含以下项目数组的对象数组:

var names = [
{items:[{name: "Name 1", age: 25}, {name: "Name 2", age: 20}]},
{items:[{name: "Name 3", age: 50}, {name: "Name 4", age: 40}]},
{items:[{name: "Name 5", age: 20}, {name: "Name 6", age: 40}]}
];

<p ng-repeat="name in names">
    <p ng-repeat="n in name.items">
        {{n.name}}
    </p>
</p>