Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular.js ng-repeat callback

I need to send a callback when ng-repeat finishes, but my solutions don't work. In my last solution I used a directive to do it.

Markup:

<div class="results" ng-model="results">
    <div class="empl-card" ng-repeat="empl in employees | filter: searchQuery" callback-on-end="alert('callback')">
    ...
    </div>
</div>

directive:

app.directive("callbackOnEnd", function() {
    return {
        restrict: "A",
        link: function(scope, element, attrs) {
            if (scope.$last) {
                scope.$eval(attrs.callbackOnEnd);
            }
        }
    };
});

Where could I have made a mistake?

like image 667
snowfinch27 Avatar asked Oct 18 '22 07:10

snowfinch27


2 Answers

I think you can do this with just ngInit as follows;

<div class="results" ng-model="results">
  <div class="empl-card" ng-repeat="empl in employees | filter: searchQuery" ng-init="$last && alert('callback')">
    ...
  </div>
</div>

You don't need a custom directive for doing this.

like image 129
Ali BARIN Avatar answered Oct 29 '22 21:10

Ali BARIN


To achieve this you need to make a method in your controller and call that method when the ng-repeat ends :

(function() {

  angular.element(document).ready(function() {
    var app = angular.module('myApp', []);

    app.controller("MyCtrl", function($scope, $timeout) {

      $scope.names = ['A', 'B', 'C', 'D'];

      $scope.onEnd = function() {
        alert('all done');
      };

    });

    app.directive("callbackOnEnd", function() {
      return {
        restrict: "A",
        link: function(scope, element, attrs) {
          if (scope.$last) {
            scope.$eval(attrs.callbackOnEnd);
          }
        }
      };
    });

    angular.bootstrap(document, ['myApp']);
  });


}());
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-controller="MyCtrl" class="well">
  <h3 ng-repeat="name in names" callback-on-end="onEnd()">{{name}}</h3>
</div>

Or you will have to eval the javascript code

app.directive("callbackOnEnd", function() {
    return {
        restrict: "A",
        link: function(scope, element, attrs) {
            if (scope.$last) {
                eval(attrs.callbackOnEnd);
            }
        }
    };
});

(function() {

  angular.element(document).ready(function() {
    var app = angular.module('myApp', []);

    app.controller("MyCtrl", function($scope, $timeout) {

      $scope.names = ['A', 'B', 'C', 'D'];

      $scope.onEnd = function() {
        alert('all done');
      };

    });

    app.directive("callbackOnEnd", function() {
      return {
        restrict: "A",
        link: function(scope, element, attrs) {
          if (scope.$last) {
            eval(attrs.callbackOnEnd);
          }
        }
      };
    });

    angular.bootstrap(document, ['myApp']);
  });


}());
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-controller="MyCtrl" class="well">
  <h3 ng-repeat="name in names" callback-on-end="alert('done')">{{name}}</h3>
</div>

Find good explanation here : https://stackoverflow.com/a/15671573/3603806

like image 37
Himanshu Tyagi Avatar answered Oct 29 '22 19:10

Himanshu Tyagi