so I have a problem with ng-repeat directive. In my code I have a parent controller which have data stored as array of objects.
$scope.queue = [
{
name: 'Mark',
sex: 'Male',
age: 21
},
{...}
];
$scope.changePositionInQueue = function (currIndex, targetIndex) {
// move up / down person in queue
};
What I want to do is pass parent controller's function to my directive's ('person') isolated scope and at the same time be able to use '$index', '$first', '$last' variables.
<person data-change-position="changePositionInQueue" data-person="person" ng-repeat="person in queue"></person>
Directive scope declaration:
scope: {
person: '=',
changePosition: '&'
}
The problem is that when I create isolated scope inside ng-repeat loop I lose ng-repeat properties. On the other hand when I create default scope by ng-repeat and I have access to all wanted properties I can't use parent function.
This is my solution to your challenge:
In view:
<my-directive dir-func="fnc($index)" data="data" ng-repeat="data in datas"><span>{{data.id|json}}</span><br></my-directive>
In Direct call parent function in 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
}
}