Update
Karl seamon gave a talk in ng-conf 2014.
In this video (22:20 minute) he talked about a future possiblity of a built-in $postDigestWatch.
Here is an open issue in: https://github.com/angular/angular.js/issues/5828
So, It will probably get to the core in future releases, until then you can use the trick below.
- A digest cycle may have multiple
$digest.
- I
$watch for the first $digest to register a $timeout which would run after the digest cycle ends.
- I must unregister the
$watch immediately to avoid multiple $timeout callbacks for one digest cycle.
- In the
$timeout callback I invoke the user callback and register a $watch for the next $digest.
Use $watch in conjunction with $timeout:
function postDigest(callback){
var unregister = $rootScope.$watch(function(){
unregister();
$timeout(function(){
callback();
postDigest(callback);
},0,false);
});
}
postDigest(function(){
console.log('do something');
})
$digest ,From the docs:
If you want to be notified whenever $digest() is called, you can register a watchExpression function with $watch() with no listener.
$timeout , From here: Defer angularjs watch execution after $digest (raising DOM event)
$timeout will cause another digest cycle to be executed after the function is executed. If your trigger does not affect anything Angular, you can set the invokeApply argument to false to avoid running another digest cycle.