angularjs:NG-repeat的“轨道由”前pression [英] angularjs: ng-repeat's 'track by' expression
问题描述
我有理解困难的是如何在按部就班地进行,前$ P $在angularjs作品NG-重复pssion。该文档是非常稀少: http://docs.angularjs.org/api/ng/directive / ngRepeat
你能解释一下code的这两个片段之间的差异来讲是什么样的绑定和其他相关方面?
与通过跟踪指数$
<! - 名字是一个数组 - >
< DIV NG重复=(键,值)名称跟踪由$指数>
<输入NG模型=值[关键]>
< / DIV>
无(相同的输出)
<! - 名字是一个数组 - >
< DIV NG重复=(键,值)名称>
<输入NG模型=值[关键]>
< / DIV>
可以通过跟踪指数$
如果您的数据源中有重复的标识符
例如: $ scope.dataSource:[{ID:1,名称:一},{ID:1,名称:一太'},{ID:2,名称:'两个}]
在使用ID作为标识符不能重复这个集合(重复ID:1)。
将不起作用:
<元素NG重复=item.id作为item.name在数据源项GT&;
//一些与项目...
< /组件>
但你可以,如果用通过跟踪指数$
:
<元素NG重复=$通过指数的数据源跟踪项目>
//一些与项目...
< /组件>
I'm having difficulties understanding how the track by expression of ng-repeat in angularjs works. The documentation is very scarce: http://docs.angularjs.org/api/ng/directive/ngRepeat
Can you explain what the difference between those two snippets of code is in terms of databinding and other relevant aspects?
with: track by $index
<!--names is an array-->
<div ng-repeat="(key, value) in names track by $index">
<input ng-model="value[key]">
</div>
without (same output)
<!--names is an array-->
<div ng-repeat="(key, value) in names">
<input ng-model="value[key]">
</div>
You can track by $index
if your data source has duplicate identifiers
e.g.: $scope.dataSource: [{id:1,name:'one'}, {id:1,name:'one too'}, {id:2,name:'two'}]
You can't iterate this collection while using 'id' as identifier (duplicate id:1).
WON'T WORK:
<element ng-repeat="item.id as item.name for item in dataSource">
// something with item ...
</element>
but you can, if using track by $index
:
<element ng-repeat="item in dataSource track by $index">
// something with item ...
</element>
这篇关于angularjs:NG-repeat的“轨道由”前pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!