2016-07-16 5 views
2

ngMap https://github.com/allenhwkim/angularjs-google-maps에서 촬영이 구문 내부 NgMap에 대한 참조를 얻는 것은 잘 작동 :는 각도 컨트롤러

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); 
     }); 
    }); 

하지만 컨트롤러를 선언에 사용하는 구문은 다른 내가 오류 얻을 :

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

을 아래 라인에서 더 이상 실패합니다 :

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

컨트롤러는 다음과 같이 선언됩니다 :

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() { 
     }); 

내가 같은 다른 명백한 장소에서 NgMap를 추가하는 시도 :.

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

또는 ActivityDetailsController을하고 $ 컨트롤러 선언 등 전 = NgMap을 주입하지만 위의 유사한 오류가 있습니다 NgMap을 참조 할 수 없다는 점에서

편집 : ngMap 종속성은 이미 답변과 비슷한 다른 파일에서 설정되었습니다. 미안하지만 이전에이 코드를 넣지 않았지만 작동하지 않는 위의 코드가 같은 위치에 있으므로 원본을 그대로 두는 것이 좋습니다.

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

나는 이것이 내가 잘못 NgMap가 NgMap이 컨트롤러 선언이나 뭔가를 사용하여 ... 또는 중 하나를 프레임 워크 대부분 내 경험 부족 주입하려고하는 방식으로하는 것입니다 있는지 확실하지 않습니다!

답변

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; 
    }); 
}); 
+0

vm.map이나 필자의 경우 self.map에서 참조 할 필요가 없습니다. 오류는 여전히 "정의되지 않은 속성 'getMap'을 읽을 수 없습니다. 따라서 NgMap이 컨트롤러 함수를 선언하는 방식으로"주입 "하지 않는 것처럼 보입니다. – Steve

+0

.controller ('ActivityDetailsController', [ '$ mdSidenav', '$ mdBottomSheet', '$ timeout', '$ log', '$ http', '$ location', '$ routeParams', 'NgMap', ActivityDetailsController ] – Abcd

+0

Abcd에서 'NgMap'을 따옴표로 묶어야합니다 (항상 사용하지 않으려 고 노력했습니다). 왜 그런가? – Steve

1

당신은 ngMap 의존성을 추가하는 것을 잊었습니다.

은 모듈이 방법으로 선언 :

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

을 그리고 그것은 작동합니다.

+0

답장을 보내 주셔서 감사합니다. 나는 한 가지 방법이 효과가 있고 다른 것은 효과가 없기 때문에 실제로 그것을 이미 가지고 있습니다. 지금이 질문을 포함 시켜서 더 분명히했습니다. 건배. – Steve