9

我正在尝试捕获 $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/

怎么可能抓住这个?

4

1 回答 1

13

为了使隔离范围被捕获$last,您需要$parent像这样使用引用父范围

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

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

于 2013-08-27T16:32:44.817 回答