NgMap 错误"could not find map"



我对Angular还很陌生,我正试图用它来集成谷歌地图:https://github.com/allenhwkim/angularjs-google-maps

我遇到的问题是,当我尝试使用NgMap.getMap()获取地图实例时,当我console.log NgMap.get-map时,我得到的是"找不到地图"。我的代码是这样的:

partials/home.html:

<h1>practice locater</h1>
<ng-map id="map" center="[40.74, -74.18]"></ng-map>

Javascript:

var app = angular.module('RIThym',['ngResource','ngRoute','ngMap']);
app.config(['$routeProvider', function($routeProvider){
    $routeProvider
    .when('/', {
        templateUrl: 'partials/home.html',
        controller: 'HomeCtrl'
    })
    .otherwise({
        redirectTo: '/'
    });
}]);
app.controller('HomeCtrl',['$scope','$resource', 'NgMap',
function($scope, $resource, NgMap){
    var Locations = $resource('/api/locations');
    Locations.query(function(locations){
        $scope.locations = locations;
    });
    var map = NgMap.getMap();
    console.log(map);
    NgMap.getMap().then(function(map){
        console.log(map);
    });
}]);

ng-map指令具有id属性时,getMap函数必须使用该id值作为其第一个参数。

HTML

<h1>practice locater</h1>
<ng-map id="map" center="[40.74, -74.18]"></ng-map>

JS-

//DO this
NgMap.getMap("map").then(function(map){
//NOT this
//NgMap.getMap().then(function(map){
    console.log(map);
});

有关更多信息,请参阅NgMap Provider API参考

最新更新