asynchronous - How to asynchronously load a google map in AngularJS? -
now have found way initialize google maps of andy joslin in initialize-google-map-in-angularjs, looking way asynchronous load google map object.
i found example of how in phonecat project.
notice how js files loaded in example: index-async.html
in jade scripts partial loaded program tried:
script(src='js/lib/angular/angular.js') script(src='js/lib/script/script.min.js') script $script([ 'js/lib/angular/angular-resource.min.js', 'js/lib/jquery/jquery-1.7.2.min.js', 'http://maps.googleapis.com/maps/api/js?key=aizasybtmi_pcxmztlx5mwfrqgbveyx-h-pdxo4&sensor=false', 'js/app.js', 'js/services.js', 'js/controllers.js', 'js/filters.js', 'js/directives.js', 'bootstrap/js/bootstrap.min.js' ], function() { // when done, execute bootstrap angular application angular.bootstrap(document, ['ofm']); });
when , go load map page get:
a call document.write() asycrononously-loaded external script ignored.
this how google maps being loaded service:
'use strict'; var app = angular.module('ofm.services', []); app.factory('googlemaps', function() { var map_id = '#map'; var lat = 46.87916; var lng = -3.32910; var zoom = 15; var map = initialize(map_id, lat, lng, zoom); return map; }); function initialize(map_id, lat, lng, zoom) { var myoptions = { zoom : 8, center : new google.maps.latlng(lat, lng), maptypeid : google.maps.maptypeid.roadmap }; return new google.maps.map($(map_id)[0], myoptions); }
it appears should returning promise recall reading. angularjs new me.
if using jquery in angularjs app, check out function returns promise
when google maps api has been loaded:
https://gist.github.com/gbakernet/828536
i able use in angularjs directive lazy-load google maps on demand. works treat:
angular.module('mapmodule') // usage: data-google-map .directive('googlemap', ['$window', function ($window) { return { restrict: 'a', link: function (scope, element, attrs) { // if google maps present initialise map if ($window.google && $window.google.maps) { initgooglemaps(); } else { loadgooglemapsasync(); } function loadgooglemapsasync() { // loadgooglemaps() == jquery function https://gist.github.com/gbakernet/828536 $.when(loadgooglemaps()) // when google maps loaded, add infobox - optional .then(function () { $.ajax({ url: "/resources/js/infobox.min.js", datatype: "script", async: false }); }) .done(function () { initgooglemaps(); }); }; function initgooglemaps() { // load google map stuff here // remember wrap scope variables inside `scope.$apply(function(){...});` } } }; }]);
Comments
Post a Comment