将函数传递给 ng-repeat 对象
Passing function into ng-repeat object
所以我对 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 创建默认范围并且我可以访问所有需要的属性时,我不能使用父函数。
这是我对你的挑战的解决方案:
在视图中:
<my-directive dir-func="fnc($index)" data="data" ng-repeat="data in datas"><span>{{data.id|json}}</span><br></my-directive>
在link中直接调用父函数:
myApp.directive('myDirective', function() {
return {
//require: 'ngModle',
scope: {
data: "=",
dirFunc: "&"
},
link: function(scope, elem, attr, ngModel) {
scope.dirFunc() // parent scope.func is called here where you get the alert
}
}
所以我对 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 创建默认范围并且我可以访问所有需要的属性时,我不能使用父函数。
这是我对你的挑战的解决方案:
在视图中:
<my-directive dir-func="fnc($index)" data="data" ng-repeat="data in datas"><span>{{data.id|json}}</span><br></my-directive>
在link中直接调用父函数:
myApp.directive('myDirective', function() {
return {
//require: 'ngModle',
scope: {
data: "=",
dirFunc: "&"
},
link: function(scope, elem, attr, ngModel) {
scope.dirFunc() // parent scope.func is called here where you get the alert
}
}