角度指令数据绑定和与ng-repeat的交互

时间:2014-11-17 01:44:29

标签: angularjs angularjs-directive

所以我对以下概念感到困惑,并希望有人可以为我清理它。请考虑以下模板代码:

<test-dir val="val"></test-dir>

假设test-dir具有以下隔离范围:

scope:{ val : '='}

我的理解是,这实现了test-dir指令中的$ scope.val变量与全局变量$scope.val之间的双向绑定。因此,如果我更改任何一个的值,另一个将受到影响。

但是,现在考虑我有以下模板代码:

<div ng-repeat="(index, val) in values">
  <test-dir index="index" val="val"></test-dir>
</div>

以及以下孤立的范围:

scope:{
  index:'=', 
  val : '='
}

突然indexval不再双向绑定。因此,我的test-dir指令中的每一个都有自己的indexval,并且在本地更改变量不会影响其他任何人。为什么呢?

1 个答案:

答案 0 :(得分:1)

ng-repeat creates new child scopes,因此每个test-dir都有自己的indexval正在按预期工作。

相关问题