0

I am creating a custom directive in a controller and calling it in ng-repeat as follows:

HTML:

<div ng-controller="TestCtrl">
  <div ng-repeat="page in pages">
   <custom 
    load-data="loadData = fn">
   </custom>
  </div>
</div>  

JS:
Test directive is as follows:

scope: {
   loadData: "&"
}
controller: ['$scope', '$element', '$timeout', '$filter', function ($scope, $element, $timeout, $filter) {
$scope.loadData({ fn: function(data) {  
 //Data calc.
}});
}  

I am calling loadData from TestCtrl as follows:

App.controller('TestCtrl', function($scope, $http, $timeout, $rootScope) {
 $scope.loadData(data);
}  

In TestCtrl scope, loadData function is present if ng-repeat is not used and works fine but gives error as undefined is not a function at line where $scope.loadData(data) is called when ng-repeat is used.
Thanks in Advance

Abhishek
  • 1,999
  • 5
  • 26
  • 52
  • 1
    Looks to be a scope issue. ngRepeat gives each template instance its own scope. [link](https://docs.angularjs.org/api/ng/directive/ngRepeat) – David Sung Lee Sep 22 '14 at 15:36
  • You may also want to consider using $broadcast and $on. [link](http://stackoverflow.com/questions/19038778/angularjs-how-to-call-child-scope-function-in-parent-scope) – David Sung Lee Sep 22 '14 at 15:39
  • I tried using $broadcast and $on, same issue, It works if ng-repeat is not provided, but if ng-repeat is present, it does not enter $on function. – Abhishek Sep 22 '14 at 16:54

1 Answers1

0

The controller of your directive is not correct, It's syntax is

 controller: function($scope, $element, $attrs, $transclude, otherInjectables) { ... }

$timeout, $filter should come after $attrs and $transculde. You need to include both even if you are not using it

Even after above changes, if there is still an issue, then we can check further, but the above has to be fixed or else you wont be able to use $timeout and $filter

Siraj
  • 687
  • 4
  • 7