【问题标题】:Google Maps Markers and AngularJS谷歌地图标记和 AngularJS
【发布时间】:2015-07-12 12:18:55
【问题描述】:

我正在使用 Ionic (AngularJS + Cordova)。
使用以下代码,我创建了带有单击事件的标记,但是当我单击标记时,$scope.example 不会在视图中更新(但在控制台中它会显示 id,函数有效)。为什么?
我认为标记不是用 AngularJS(?) 编译的。

$scope.setInfoWindow = function(id){$scope.example = id;console.log(id)}

for(i=0;i<data.length;i++){
    marker = new google.maps.Marker({
        position: new google.maps.LatLng(data[i].latit,data[i].longi),
        map: map,
        animation: google.maps.Animation.DROP
    });
    google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
            $scope.setInfoWindow(data[i].id);
        }
    })(marker, i));
}

【问题讨论】:

    标签: angularjs google-maps google-maps-api-3 angularjs-scope google-maps-markers


    【解决方案1】:

    Angular 作用域在处理事件/通过事件操作作用域时不运行其摘要循环,此时您需要手动运行摘要循环。

    范围变量更新完成后,您需要手动运行摘要循环。这样它就会更新视图上的绑定。

    $scope.setInfoWindow = function(id){
       $scope.example = id;
       console.log(id)
       $scope.$apply(); //running digest cycle to update binding
    }
    

    【讨论】:

    • 谢谢,这正是我想要的。
    • 很高兴为您提供帮助..谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多