Created
February 24, 2014 13:52
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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