0

所以我对ng-repeat指令有问题。在我的代码中,我有一个父控制器,其数据存储为对象数组。将函数传递给ng-repeat对象

$scope.queue = [ 
    { 
    name: 'Mark', 
    sex: 'Male', 
    age: 21 
    }, 
    {...} 
]; 

$scope.changePositionInQueue = function (currIndex, targetIndex) { 
    // move up/down person in queue 
}; 

我想要做的是通过父控制器对我的指令的(“人”)分离范围,并在同一时间可以使用“$索引”功能,“$第一”,“$最后”变量。

<person data-change-position="changePositionInQueue" data-person="person" ng-repeat="person in queue"></person> 

指令范围声明:

scope: { 
person: '=', 
changePosition: '&' 
} 

的问题是,当我创建NG-REPEAT循环内隔离范围我失去了NG-重复属性。另一方面,当我通过ng-repeat创建默认范围并且我可以访问所有想要的属性时,我不能使用父功能。

+0

所以即使通过那些范围为参数隔离示波器指令 – harishr

+0

感谢您的回答,但我该怎么做?以参数方式传递参数。$ index或$ index在指令子作用域中给我'未定义'。 – tomeks

+0

类似'index:@'在你的指令定义范围内,然后在html'data-index =“$ index”'应该做 – harishr

回答

0

这是我的解决您的挑战:
鉴于:

<my-directive dir-func="fnc($index)" data="data" ng-repeat="data in datas"><span>{{data.id|json}}</span><br></my-directive> 

直接调用父功能链接:

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 

    } 

    } 

See the Plunker for detail

+0

谢谢你的回答。它没有帮助我直接因为我通过从$父范围获得$ index属性,但我的功能仍然无法正常工作。我通过查看你的指令发现了一个错误。我在我的指令中通过引用传递函数,但没有提供有关所需参数的信息。无论如何,问题解决了,非常感谢! – tomeks

+0

很高兴帮助,例子的目的就是为了这个! :)欢呼! – praHoc