简体   繁体   English

角谷歌地图单击事件:无法读取未定义的属性“单击”

[英]Angular google maps click event: Cannot read property 'click' of undefined

I'm using angular google maps with ionic framework and trying to get coordinates on click. 我正在使用带有离子框架的Google角形地图 ,并试图在单击时获得坐标。 In view directive is injected like so 在视图指令是这样注入的

<ui-gmap-google-map center="map.center" zoom="map.zoom" dragging="true" options="map.options" events="map.events"></ui-gmap-google-map>

And here is my controller part 这是我的控制器部分

angular.extend($scope,

  $scope.map = {
    center: {latitude: 53.902407, longitude: 27.561621 },
    zoom: 15,
    options: {
      maxZoom: 16,
      minZoom: 13,
      styles: mapStyles.bright
    },
    events: {
      click: function (mapModel, eventName, originalEventArgs) {
        $log.info("user defined event: " + eventName, mapModel, originalEventArgs);

        var e = originalEventArgs[0];
        var lat = e.latLng.lat(),
          lon = e.latLng.lng();
        console.log('You clicked here ' + 'lat: ' + lat + ' lon: ' + lon);
        //scope apply required because this event handler is outside of the angular domain
        $scope.$apply();
      }
    }
  });

Event part is basically copy-pasted from examples in docs. 事件部分基本上是从docs中的示例复制粘贴的。 However, when I click it, I get an error 但是,当我单击它时,出现错误

在此处输入图片说明

Where did I go wrong? 我哪里做错了?

UPD Somehow $scope.map.events is undefined, although I can, for instance, console.log it and see that it's defined before the error occurs. UPD某种程度上$ scope.map.events是未定义的,尽管例如,我可以console.log记录它,并查看它是在错误发生之前定义的。 Same error is happening for all events. 所有事件都发生相同的错误。 For example, 例如,

 events: {
        tilesloaded: function (map) {
            $scope.$apply(function () {
                $scope.mapInstance = map;
                $log.info('tiles loaded');
            });
        }
 }

returns 退货

Uncaught TypeError: Cannot read property 'tilesloaded' of undefined

in console with the same stacktrace. 在具有相同stacktrace的控制台中。

You have mixed two ways of bringing something into scope; 您已经混合了两种方法来将某些东西纳入范围: assignment to scope and extending scope. 范围分配和扩展范围。 Either extend the $scope with an object (ie not $scope.map = ) and your code becomes... 用对象扩展$ scope(即不是$scope.map = ),您的代码将变为...

angular.extend($scope,{
  map: {
    center: {latitude: 53.902407, longitude: 27.561621 },
    zoom: 15,
    options: {
      maxZoom: 16,
      minZoom: 13,
      styles: mapStyles.bright
    },....
})

or use assignment to scope 或使用赋值作用域

$scope.map = {
  center: {latitude: 53.902407, longitude: 27.561621 },
  zoom: 15,
  options: {
    maxZoom: 16,
    minZoom: 13,
    styles: mapStyles.bright
  },....

http://moduscreate.com/angularjs-tricks-with-angular-extend/ http://moduscreate.com/angularjs-tricks-with-angular-extend/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM