Last active
November 5, 2015 09:12
-
-
Save eloone/c5d32dfca8bf923f72eb to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
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