角度谷歌地图更新标记模型 [英] angular-google-maps updating marker model

查看:146
本文介绍了角度谷歌地图更新标记模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用angular-google-maps库,但当我动态地将新标记添加到我的模型时,似乎无法获取地图更新。



HTML

 < ; ui-gmap-markers models =map.placeMarkerscoords ='self'icon ='icon'options ='options'click ='onClicked'modelsbyref =true>< / UI-GMAP标记物> 

JS

  $ scope.map.placeMarkers = [
{
id:1,
latitude:45,
longitude:-74,
options:{} ,
title:'Test 123'
},
{
id:2,
纬度:15,
经度:30,
选项:{},
title:'Test 1234'
}
];

上面的工作正常,但是当我这样做时:

  var place = {
id:3,
latitude:455,
longitude:-574,
options:{} ,
title:'Test 1233455'
};

$ scope.map.placeMarkers.push(place);

模型是更新的,但不是地图。我曾尝试使用$ scope。$ apply(),但由于摘要循环已在进行中,因此出现错误。

解决方案

发生在我身上。花费几个小时寻找解决方案。



好像每当我们更新标记时,angularjs都不会立即更新它。只需在将新标记推送到数组之后调用 $ scope。$ digest()即可。

同时检查 $ scope。$ apply()函数,它会自动调用$ scope。$ digest()。
请参考本教程详细解释:
教程

它写在上面的教程:


您可能会遇到一些AngularJS不会为您调用$ digest()函数的情况。您通常会通过注意到数据绑定不会增加显示的值来检测该数据。在这种情况下,调用$ scope。$ digest(),它应该可以工作。或者,您可以使用$ scope。$ apply()



Im using the angular-google-maps library but I cannot seem to get the map to update when I dynamically add new markers to my model. Hardcoded markers in the model are correctly displayed.

HTML

<ui-gmap-markers models="map.placeMarkers" coords="'self'" icon="'icon'" options="'options'" click="'onClicked'" modelsbyref="true"></ui-gmap-markers>

JS

$scope.map.placeMarkers = [
            {
                id: 1,
                latitude: 45,
                longitude: -74,
                options: {},
                title: 'Test 123'
            },
            {
                id: 2,
                latitude: 15,
                longitude: 30,
                options: {},
                title: 'Test 1234'
            }
        ];

The above is working fine however when I do:

var place = {
   id: 3,
   latitude: 455,
   longitude: -574,
   options: {},
   title: 'Test 1233455'
};

$scope.map.placeMarkers.push(place);

The model is update but not the map. I have tried using $scope.$apply() but got an error since a digest cycle was already in progress.

解决方案

Same happened with me. Spent hours to find solutions.

It seemed like whenever we update markers angularjs doesn't update it immediately. just call $scope.$digest() after pushing new marker to the array.

Also check $scope.$apply() function, it calls $scope.$digest() automatically. Please refer to this tutorial which explain in detail: tutorial showing $scope.$digest(), $scope.$apply, $scope.watch

Its written in above tutorial:

You may encounter some corner cases where AngularJS does not call the $digest() function for you. You will usually detect that by noticing that the data bindings do not upate the displayed values. In that case, call $scope.$digest() and it should work. Or, you can perhaps use $scope.$apply()

这篇关于角度谷歌地图更新标记模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆