【发布时间】:2016-11-28 20:11:41
【问题描述】:
所以我对 ng-repeat 指令有疑问。在我的代码中,我有一个父控制器,它的数据存储为对象数组。
$scope.queue = [
{
name: 'Mark',
sex: 'Male',
age: 21
},
{...}
];
$scope.changePositionInQueue = function (currIndex, targetIndex) {
// move up / down person in queue
};
我想要做的是将父控制器的功能传递给我的指令的('person')隔离范围,同时能够使用'$index'、'$first'、'$last'变量。
<person data-change-position="changePositionInQueue" data-person="person" ng-repeat="person in queue"></person>
指令范围声明:
scope: {
person: '=',
changePosition: '&'
}
问题是当我在 ng-repeat 循环中创建隔离范围时,我会丢失 ng-repeat 属性。另一方面,当我通过 ng-repeat 创建默认范围并且我可以访问所有想要的属性时,我无法使用父函数。
【问题讨论】:
-
所以甚至将作用域上的那些作为参数传递给您的隔离作用域指令
-
感谢您的回答,但我该怎么做呢?将参数作为 person.$index 或 $index 传递给我在指令子范围内的“未定义”。
-
在你的范围指令定义中类似于
index: @,然后在htmldata-index="$index"上应该这样做
标签: javascript angularjs angularjs-directive angularjs-scope ng-repeat