Last Updated: February 25, 2016
·
16.78K
· 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>

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside