gpt4 book ai didi

angularjs - NgMap错误 "could not find map"

转载 作者:行者123 更新时间:2023-12-02 06:42:47 25 4
gpt4 key购买 nike

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

我遇到的问题是当我尝试使用 NgMap.getMap() 获取 map 实例时,当我 console.log NgMap.getMap 时,我收到“无法找到 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 Reference

关于angularjs - NgMap错误 "could not find map",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37213835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com