Skip to content

Instantly share code, notes, and snippets.

@wholypantalones
Created August 24, 2015 19:58
Show Gist options
  • Save wholypantalones/9a517b9d222875b9341b to your computer and use it in GitHub Desktop.
Save wholypantalones/9a517b9d222875b9341b to your computer and use it in GitHub Desktop.
Lazyload Google Maps Angular Service
/*
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