简体   繁体   中英

Google map doesn't show on second partial view with angularjs

I've been following the answers/examples in this question here. Initialize Google Map in AngularJS

I've setup a plunker example of whats happening. http://plnkr.co/edit/5fN7PR

Including the git repo example. My map loads when I first view the map page (partial) however if I select another link then go back to the map the map doesn't load. I can see the map still exists as its being called its just not replacing the div with id="map".

I don't care that the map will be reinitialized at this stage, I've setup a store for the maps for different places, however that won't work without the map! My list and details pages work as expected. /web/place is a ajax call to my silex backend.

index.html

<div class="container full-width" ng-view></div>

app.js

'use strict';

/* App Module */
var app = angular.module('places', ['Controllers', 'Filters', 'Services']);

app.config(['$routeProvider', function($routeProvider) {
  $routeProvider.
      when('/places', {templateUrl: 'partials/place-list.html',   controller: 'ListController'}).
      when('/places/:placeId/details', {templateUrl: 'partials/place-detail.html', controller: 'DetailController'}).
      when('/places/:placeId/map', {templateUrl: 'partials/place-map.html', controller: 'MapController'}).
      otherwise({redirectTo: '/places'});
}]);

controllers.js

'use strict';

/* Controllers */
var controllers;
controllers = angular.module("Controllers", []);

controllers.controller("ListController", function($scope, Place) {
    $scope.places = Place.query();
});

controllers.controller("DetailController", function($scope, $routeParams, Place) {
    $scope.place = Place.get({placeId: $routeParams.placeId});
});

controllers.controller("MapController", function($scope, $routeParams, Place, GoogleMaps) {
    $scope.place = Place.get({placeId: $routeParams.placeId});
    $scope.map = GoogleMaps;
});

services.js

'use strict';

/* Services */
var services;

services = angular.module('Services', ['ngResource']);

services.factory('Place', function($resource){
    return $resource('/web/place/:placeId', {}, {
        query: {method:'GET', params:{placeId:'list'}, isArray:true}
    });
});


services.factory('GoogleMaps', function() {
    var map_id  = '#map';
    var lat     = -25;
    var lng     = 133;
    var zoom    = 16;
    var map     = initialize(map_id, lat, lng, zoom);

    console.log(map);
    return map;

    function initialize(map_id, lat, lng, zoom) {
        var myOptions = {
            zoom : zoom,
            center : new google.maps.LatLng(lat, lng),
            mapTypeId : google.maps.MapTypeId.ROADMAP
        };

        console.log(myOptions);

        return new google.maps.Map($(map_id)[0], myOptions);
    }
});

place-map.html

<div id="map" style="width: 100%;"></div>

I've started using angularjs UI. It has solved my problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM