我构建了一个Angular应用程序,我正在使用ng-repeat
将模型中的数据显示到html视图中。但我得到了错误的价值观:
模型看起来像这样:
[{
id : 1,
name : 'jijo'
address : 'addressone',
},
{
id : 2,
name : 'albert'
address : 'addressone',
},
{
id : 3,
name : 'moana'
address : 'addressone',
},
{
id : 4,
name : 'card'
address : 'addressone',
}
]
Html代码如下所示:
<section id="main" ng-repeat="(id, name) in data.repo track by id"
<div id="sub_main_one">
{{id}} //prints one
</div>
<div id="sub_main_two">
{{id}} //prints one
</div>
<div id="sub_main_three">
{{id}} //prints one
</div>
<div id="sub_main_four">
{{id}} //prints one
</div>
<div id="sub_main_five">
{{id}} //suppose to be one but prints 4 on the first repeat
</div>
</section>
id在大多数div中打印1,但在最后一个div中它给了我一个错误的值(4),这是来自数组中最后一个对象的id。我该如何解决这个问题?..
答案 0 :(得分:1)
您的代码中遗漏了>
,但您的ng-repeat
句子错误,(key, value) in obj
sintax将迭代对象的属性,而不是迭代项目一个数组(查看angularjs文档):
修复:
<section id="main" ng-repeat="item in data">
<div id="sub_main_one">
{{item.id}}
hey
</div>
<div id="sub_main_two">
{{id}}
</div>
<div id="sub_main_three">
{{id}}
</div>
<div id="sub_main_four">
{{id}}
</div>
<div id="sub_main_five">
{{id}}
</div>
</section>
这是plunker