Last Updated: February 25, 2016
·
16.25K
· gus

Execute function when ngRepeat done

ngRepeat is one of Angular's most powerful directives, sometimes you might find yourself wanting to update the DOM or execute a function after the directive has finished its job.

We can use a custom directive and $destroy() to accomplish this.

var app = angular.module("app", []);

app.directive('repeatDone', function() {
  return function(scope, element, attrs) {
    element.bind('$destroy', function(event) {
      if (scope.$last) {
        scope.$eval(attrs.repeatDone);
      }
    });
  }
});

Then we can use it along with ngRepeat in the following fashion.

<div ng-repeat="item in model.items" repeat-done="foo()"></div>