Initialize Google Map in AngularJS

后端 未结 4 1153
一个人的身影
一个人的身影 2020-12-07 19:17

I am considering migrating from backbonejs to angularjs.

In backbone I am able to initialize a view at which point I create an instance of google map. I can then pa

4条回答
  •  失恋的感觉
    2020-12-07 19:52

    Since views create and destroy controllers when the view changes, you want your maps data to persist somewhere else. Try creating a GoogleMap service which will store the data.

    myApp.factory('GoogleMaps', function() {
      var maps = {};
    
      function addMap(mapId) {
        maps[mapId] = {};
      }
      function getMap(mapId) {
        if (!maps[mapId]) addMap(mapId);
        return maps[mapId];
      }
    
      return {
        addMap: addMap,
        getMap: getMap
      }
    });
    
    function MyController($scope, GoogleMaps) {
      //Each time the view is switched to this, retrieve supermanMap
      $scope.map = GoogleMaps.getMap('supermanMap');
    
      $scope.editMap = function() {
        $scope.map.kryptonite = true;
      };
    }
    

    If you don't like this solution, there are some other ways to do it too.

提交回复
热议问题