我正在使用 Google Maps API 和 AngularJS。我有以下函数可以使用 setOptions
更改多边形的颜色:
desk.modColor = function (color) {
desk.setOptions({ fillColor: color });
}
我在 angularJS 中使用如下代码调用它:
$scope.focusedDesk.modColor('#0592fa');
但是,这仅在大约 75% 的时间内有效。在 AngularJS 中进行足够的范围操作后,这将停止工作。如果我在 modColor
调用后立即 console.log
desk
对象,它有正确的 fillColor
,但是 map 上的多边形在视觉上没有更新。
知道为什么会这样吗?有什么方法可以确保 setOptions
以可视方式更新 map /多边形吗?谢谢。
最佳答案
条件一
如果你在 angularJS 中以任何形式操作你的 DOM,你应该使用指令 在这种情况下,您应该编写一个指令来为您执行此操作:
angular.module('myModule',[])
.directive('myDirective',function(){
return {
scope: { focusedDesk:'=' },
link: function(scope, iElement, iAttrs){
scope.$watch('your-whatever-object-or-property-you-want-to-watch-over', function(){
//your desired action
})
}
}
})
用法是这样的:
<div my-directive focusedDesk='focusedDesk'></div>
条件2
如果您只关心属性的值,您可以在 controller
函数中使用 $scope.$watch
相同的方法
关于javascript - 设置选项后谷歌地图多边形不更新颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18197267/