Created
August 24, 2015 19:58
-
-
Save wholypantalones/9a517b9d222875b9341b to your computer and use it in GitHub Desktop.
Lazyload Google Maps Angular Service
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
/* | |
usage: | |
Initializer.mapsInitialized.then(function(){ | |
initMap(); // do stuff with maps | |
}); | |
*/ | |
angular.module('ngApp', []). | |
factory('Initializer', function($window,$q){ | |
var asyncUrl = '//maps.googleapis.com/maps/api/js?callback=', | |
mapsDefer = $q.defer(); | |
$window.googleMapsInitialized = mapsDefer.resolve; | |
var asyncLoad = function(asyncUrl, callbackName) { | |
var script = document.createElement('script'); | |
script.src = asyncUrl + callbackName; | |
document.body.appendChild(script); | |
}; | |
asyncLoad(asyncUrl, 'googleMapsInitialized'); | |
return { | |
mapsInitialized : mapsDefer.promise | |
}; | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment