简体   繁体   English

AngularJS:带有隔离范围和$ last的指令中的ng-repeat

[英]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. 我试图抓住$ last属性,以便在ng-repeat完成时收到通知。 I've created special directive for that ( ngRepeatDoneNotification ). 我已经为此创建了特殊指令( ngRepeatDoneNotification )。 Ng-repeat applies to another element directive ( unit ). Ng-repeat适用于另一个元素指令( 单位 )。 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. 一旦我将范围设置为隔离,$ last就会在我的通知程序指令中消失。

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/ 我创建了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 为了使隔离范围捕获$last ,您需要使用$parent来引用父范围,如下所示

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. 您可以重构它以使其适用于两种情况或只是简单地复制它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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