在角度控制器中獲取NgMap的引用。

[英]Getting a reference to NgMap inside Angular controller


This syntax taken from ngMap https://github.com/allenhwkim/angularjs-google-maps works fine:

這個語法取自ngMap https://github.com/allenhwkim/angularjs-google- google地圖。

    angular
    .module('trails')
    .controller('MyController', function (NgMap) {
        NgMap.getMap().then(function (map) {
            console.log(map.getCenter());
            console.log('markers', map.markers);
            console.log('shapes', map.shapes);
        });
    });

But the syntax I use for declaring controllers is different and I get the error:

但是我用於聲明控制器的語法不同,我得到的錯誤是:

angular.js:13642 TypeError: Cannot read property 'getMap' of undefined

角。無法讀取未定義屬性“getMap”

It fails on the line as further below:

它在下面的線上失敗:

NgMap.getMap().then(function (map) {

NgMap.getMap()。然后(函數(地圖){

The controller is declared as:

財務主任聲明為:

    angular
    .module('trails')
    .controller('ActivityDetailsController', [
        '$mdSidenav', '$mdBottomSheet', '$timeout', '$log', '$http', '$location', '$routeParams',
        ActivityDetailsController
    ]);
function ActivityDetailsController($mdSidenav, $mdBottomSheet, $timeout, $log, $http, $location, $routeParams, NgMap) {
    var self = this;

    $http.get('/activity/detailStats/' + $routeParams.id, {
        params: { max: "1000" }
    })
        .success(function (data) {
            self.stats = data.stats;

            // Zoom to fit
            var bounds = new google.maps.LatLngBounds();
            for (var i = 0; i < self.stats.activityTrackPoints.length; i++) {
                var latlng = new google.maps.LatLng(self.stats.activityTrackPoints[i][0], self.stats.activityTrackPoints[i][1]);
                bounds.extend(latlng);
            }
            NgMap.getMap().then(function (map) {
                map.setCenter(bounds.getCenter());
                map.fitBounds(bounds);
            });

        })
        .error(function (data, status) {
            console.error('https error', status, data);
        })
        .finally(function () {
        });

I have tried adding the NgMap in other obvious places, such as:

我嘗試在其他明顯的地方添加NgMap,例如:

...'$routeParams', NgMap,
        ActivityDetailsController...

or doing a ActivityDetailsController.$inject = NgMap before the controller declaration etc but it gives a similar error to above in that NgMap cannot be referenced.

或者做一個ActivityDetailsController。$inject = NgMap之前的控制器聲明等等,但是在無法引用的NgMap中,它會產生與上面類似的錯誤。

Edit: ngMap dependency was setup in another file similar to the answers already. Sorry I didn't put this earlier, but the above code that works and doesn't work are in the same place together so I thought it seemed ok to leave that out originally.

編輯:ngMap依賴項是在另一個文件中設置的,類似於已經給出的答案。不好意思,我之前沒講過這個,但是上面的代碼是有效的,而不是無效的,所以我覺得把它放在一起是可以的。

var app = angular
.module('trails', ['ngMaterial', 'md.data.table', 'ngRoute', 'ngMap'])

I'm not sure if this is to do with the way I am incorrectly trying to inject NgMap using this controller declaration or something in NgMap...or most likely my inexperience with either framework!

我不確定這是否與我使用這個控制器聲明或NgMap中的某些東西不正確地嘗試注入NgMap有關……或者很可能是我對這兩個框架的缺乏經驗!

2 个解决方案

#1


1  

var app= angular.module('trails', ['ngMap']);
  app.controller('MyController', function($scope, $interval, NgMap) {
    var vm = this;
    NgMap.getMap().then(function(map) {
      vm.map = map;
    });
});

#2


1  

You forgot to add ngMap dependency.

您忘記添加ngMap依賴項了。

Declare your module this way:

以這種方式聲明你的模塊:

angular.module('trails', ['ngMap'])

And it should work.

它應該工作。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/07/16/7251ac44915fdc88d93356b165a586c9.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com