【发布时间】:2014-02-16 03:00:59
【问题描述】:
我在 $scope 中有一个项目树,我想在其上使用 ng-repeat 进行迭代,并根据节点类型包含不同的模板。这棵树有点像
$scope.item = {
ID: 1,
children: {
1: {
ID: 11,
},
2: {
ID: 12,
}
},
};
问题是当我做类似的事情时
<div ng-repeat="item in item.children">
item.ID : {{item.ID}}
<div ng-include="showVars(item.ID)"></div>
</div>
showVars 函数的参数是在父范围内评估的 item.ID 值,而不是由 ng-repeat 创建的子范围。当使用{{item.ID}} 显示时,该值是正确的。
this Fiddle 中的示例。
我的理解是调用函数时子作用域中的值还没有更新,对吗?这是 AngularJS 的正常行为还是错误?
编辑:
为了更明确,我希望在小提琴中
calling getTemplate with $scope.item.ID = 12 and itemID = 12
而不是
calling getTemplate with $scope.item.ID = 1 and itemID = 12
因为 ng-repeat 应该为 $scope.item 分配变量。
谢谢,
【问题讨论】:
-
我不明白问题出在哪里。所有日志都显示该方法收到了正确的项目 ID:“and itemID = 12”。并且包含的模板还显示子 ID:“My Template with item.ID = 12”。
-
对不起,我应该更明确一点:问题是,正如您在日志中看到的那样,当评估 ng-include 时,$scope.item 仍然与父作用域中的相同,而我希望它是由 ng-repeat 创建的子范围的 $scope.item。为了更明确,我希望
calling getTemplate with $scope.item.ID = 12 and itemID = 12而不是calling getTemplate with $scope.item.ID = 1 and itemID = 12
标签: angularjs angularjs-ng-repeat ng-repeat