Skip to content

Instantly share code, notes, and snippets.

@kennethlynne
Created February 24, 2014 13:52
Show Gist options
  • Save kennethlynne/9188801 to your computer and use it in GitHub Desktop.
Save kennethlynne/9188801 to your computer and use it in GitHub Desktop.
Trigger event when angular has finished loading and $http has no pending requests
'use strict';
angular.module('theApp')
.directive('onReady', function ($rootScope, $http, $timeout) {
return {
restrict: 'A',
link: function ($scope) {
console.log('Waiting.');
var to;
function resetTimer() {
//Reset counter for every digest cycle
$timeout.cancel(to);
to = $timeout(function () {
if($http.pendingRequests > 0) {
console.log('Wait..');
resetTimer();
}
console.log('Application ready');
listener(); //Stop watching
$rootScope.$broadcast('ready');
}, 2000);
}
var listener = $scope.$watch(function () {
console.log('digest');
resetTimer();
});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment