2016-09-23 50 views
1

我使用NgMap和angular 1来显示谷歌地图,并在其上绘制各种形状。 我试图通过改变一个范围变量动态地改变形状的颜色。ng-map动态改变形状的颜色

在模板中,我有:

<shape id="circle" name="circle" fill-color='{{circle.color}}' stroke-color='{{circle.color}}' stroke-opacity="0.8" stroke-weight="2" center="[41,-87]" radius="4000" editable="false"> 
</shape> 

,并在控制器中创建对象:

function CircleColorTestController($scope, $interval) { 
    $scope.circle = { 
     color: '#00FF00' 
    }; 

    var colors = ['#FF0000', '#00FF00', '#0000FF']; 
    var i = 0; 

    $interval(function() { 
     $scope.circle.color = colors[i]; 
     console.log('Changing color to: ' + $scope.circle.color); 
     ++i; 
     if (i > 2) { 
      i = 0; 
     } 
    }, 1000); 
} 

看看这个plunkr: https://plnkr.co/edit/nx5i5h

圆的颜色应每秒都会改变,但它仍然是绿色的。 NgMap有可能吗?这是一个错误吗?

谢谢!

+0

没有错误在你的代码,填充颜​​色属性正在改变。但它没有被改变,因为它不是一个CSS属性。填充颜​​色用作配置attritube。 –

+1

好的,谢谢,那么它不是最好的api。它应该是一致的,要么所有参数都应该提供实时绑定,或者不提供任何绑定。 (例如,绑定位置或半径工作) – nagyf

回答

1

这可能是可能的,fill-color不是angularjs指令,这就是为什么它不提供实时与范围变量绑定。

下面是创建功能,你想

angular.module('app', ['ngMap']) 
 
    .controller('CircleColorTestController', CircleColorTestController); 
 

 
CircleColorTestController.$inject = ['$scope', '$interval']; 
 

 
function CircleColorTestController($scope, $interval) { 
 
    $scope.circle = { 
 
    color: 'red' 
 
    }; 
 
    var colors = ['#FF0000', '#00FF00', '#0000FF']; 
 
    var i = 0; 
 
    $interval(function() { 
 
    $scope.circle.visible = colors[i]; 
 
    ++i; 
 
    if (i > 2) { 
 
     i = 0; 
 
    } 
 
    }, 1000); 
 
}
<script data-require="[email protected]" data-semver="1.5.8" src="https://code.angularjs.org/1.5.8/angular.js"></script> 
 
<script data-require="[email protected]*" data-semver="1.7.12" src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/scripts/ng-map.min.js"></script> 
 
<link rel="stylesheet" href="style.css" /> 
 
<div ng-app="app" ng-controller="CircleColorTestController"> 
 
    <div map-lazy-load="https://maps.google.com/maps/api/js"> 
 
    <ng-map center="41,-87" zoom="11"> 
 
     <shape id="circle" ng-if="circle.visible == '#FF0000'" name="circle" fill-color='#FF0000' stroke-color='#FF0000' stroke-opacity="0.8" stroke-weight="2" center="[41,-87]" radius="4000" editable="false"> 
 
     </shape> 
 

 
     <shape id="circle" ng-if="circle.visible == '#0000FF'" name="circle" fill-color='#0000FF' stroke-color='#0000FF' stroke-opacity="0.8" stroke-weight="2" center="[41,-87]" radius="4000" editable="false"> 
 
     </shape> 
 

 
     <shape id="circle" ng-if="circle.visible == '#00FF00'" name="circle" fill-color='#00FF00' stroke-color='#00FF00' stroke-opacity="0.8" stroke-weight="2" center="[41,-87]" radius="4000" editable="false"> 
 
     </shape> 
 

 
    </ng-map> 
 
    </div> 
 
</div>

+1

我接受了您的答案,但在我的应用程序中我选择了不同的解决方案。我使用'NgMap.getMap(...)'获得地图实例,并在地图实例的形状数组中找到具有正确ID的圆形实例,并调用'circle.set('fill-color','# FF0000');'在实例上。 – nagyf