Batched digest
47 Lower River st
Apt 326
HTML
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.js"></script>
<div ng-app="myApp">
<h2>All digests logged in console...</h2>
<div ng-controller="myController">
<button ng-click="runEvents('doApply')">
runEvents "apply"
</button>
<button ng-click="runEvents('doApplyAsync')">
runEvents "async"
</button>
<div parent-directive>
<div ng-repeat="i in repeatArr" some-directive>
The repeated directive
</div>
</div>
</div>
</div>
JavaScript
var myApp = angular.module('myApp', []);
myApp.controller('myController', [ '$scope',
function($scope){
var digestsCalled = 0;
$scope.repeatArr = [0,1,2,3,4,5,6,7,8,9];
$scope.$watch(function() {
digestsCalled++;
console.log("Digests called: ", digestsCalled)
});
$scope.runEvents = function(type){
$scope.$broadcast('runEvents', type);
};
}
]);
myApp.directive('parentDirective', function(){
return {
scope: {
applyTrigger: '=',
applyAsyncTrigger: '='
},
link: function(scope, element){
scope.$on('runEvents', function(e, type){
angular.forEach(element.children(), function(child){
triggerHandlers(type, child);
});
});
function triggerHandlers(type, child){
angular.element(child).triggerHandler(type);
}
}
};
});
myApp.directive('someDirective', function(){
return {
link: function(scope, element){
element.on('doApply', function(){
console.log('working... applyTrigger');
scope.$apply();
});
element.on('doApplyAsync', function(){
console.log('working... applyAsyncTrigger');
scope.$applyAsync();
});
}
};
});