最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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 /

How is it possible to catch this?

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/4erLA/1/

How is it possible to catch this?

Share Improve this question asked Aug 27, 2013 at 16:14 acidronacidron 4174 silver badges8 bronze badges 1
  • I don't think you can have a dynamic id attribute like that. – holographic-principle Commented Aug 27, 2013 at 16:35
Add a ment  | 

1 Answer 1

Reset to default 13

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.

发布评论

评论列表(0)

  1. 暂无评论