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

时间:2013-08-27 16:14:43

标签: javascript angularjs angularjs-directive angularjs-scope

我正试图抓住$ last属性,以便在ng-repeat结束时收到通知。我为此创建了特殊指令( ngRepeatDoneNotification )。 Ng-repeat适用于另一个元素指令( unit )。因此,有一个带有三个指令的构造:

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

一旦我将范围设置为隔离,$ 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) {}
  }
});

我创建了jsFiddle http://jsfiddle.net/4erLA/1/

怎么可能抓住这个?

1 个答案:

答案 0 :(得分:13)

为了使隔离范围能够捕获$last,您需要使用$parent来引用父范围,如下所示

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

您可以重构它以使其适用于两种情况,或者只是简单地复制它。

相关问题