简体   繁体   中英

AngularJS: ng-repeat in directive with isolated scope and $last

I am trying to catch $last property to be notified when ng-repeat finishes off. I've created special directive for that ( ngRepeatDoneNotification ). Ng-repeat applies to another element directive ( unit ). So, there is a construction with three directives:

<unit ng-repeat="unit in collection" ng-repeat-done-notification id="{{unit.id}}"></unit>

Once I set scope to be isolated, $last disappeared in my notifier directive.

App.directive('ngRepeatDoneNotification', function() {
  return function(scope, element, attrs) {
    if (scope.$last){ // ISSUE IS HERE
      window.alert("im the last!");
    }
  };
});

App.directive('unit', function() {
  return {
    restrict: 'E',
    replace: true,
    scope: {id: '@'}, // ONCE I ISOLATE SCOPE, $last DISAPPEARED
    templateUrl: '/partials/unit.html',
    link: function(scope, element) {}
  }
});

I've create jsFiddle http://jsfiddle.net/4erLA/1/

How is it possible to catch this?

In order to make the isolated scope to catch $last , you need to use $parent to refer to the parent scope like this

if (scope.$parent.$last) {
    $rootScope[attrs.ngRepeatDoneNotification] = "$last has been catched"
}

You may refactor it to make it work for both scenario or just simply duplicate it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM