Skip to content

Instantly share code, notes, and snippets.

@eloone
Last active November 5, 2015 09:12
Show Gist options
  • Save eloone/c5d32dfca8bf923f72eb to your computer and use it in GitHub Desktop.
Save eloone/c5d32dfca8bf923f72eb to your computer and use it in GitHub Desktop.
/*
Usage:
$onceRendered(function(){ // do your thing});
*/
(function(angular){
'use strict';
angular.module('app.lib.onceRendered', [])
.factory('$onceRendered', ['$U', '$http', '$timeout', ServiceFactory]);
function ServiceFactory($U, $http, $timeout){
function Service(){
return function(callback){
function onDomRendered() {
if($http.pendingRequests.length > 0) {
$timeout(onDomRendered); // Wait for all templates to be loaded
} else {
//the code which needs to run after dom rendering
callback();
}
}
$timeout(onDomRendered); // Waits for first digest cycle
};
}
return new Service();
}
})(window.angular);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment