Looking to create a callback method that gets called every time ng-repeat is triggered.
After doing some research, I found a couple of options:
1. Using ng-init with ng-repeat
This approach involves calling the method using the ng-init property:
<ol ng-repeat="record in records | orderBy: sortCol:true" ng-init="$last && callMethod()">
2. Creating a custom directive
HTML:
<ol ng-repeat="record in records| orderBy: 'name'" on-last-repeat>
AngularJS:
angular.module('myapp', [])
.directive('onLastRepeat', function() {
return function(scope, element, attrs) {
if (scope.$last) setTimeout(function(){
scope.$emit('onRepeatLast', element, attrs);
}, 1);
};
})
.controller('Ctrl', function($scope, $http) {
$scope.records = [];
$scope.$on('onRepeatLast', function(scope, element, attrs){
//perform your actions here
});
$http.get('/path/to/record.json')
.success(function(data) {
$scope.records = data;
})
});
However, both methods only trigger once initially (as implied by the name ng-init). I need the method to be called whenever there is a change in the sorting column, order, or the data itself. Unfortunately, these solutions do not address my specific needs.
Any help or suggestions would be greatly appreciated!