ng-map动态更改形状的颜色

时间:2016-09-23 12:05:38

标签: angularjs ng-map

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

在模板中我有:

<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吗?这是一个错误吗?

谢谢!

2 个答案:

答案 0 :(得分: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="angular.js@1.5.8" data-semver="1.5.8" src="https://code.angularjs.org/1.5.8/angular.js"></script>
<script data-require="ng-map@*" 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 :(得分:0)

nagyf的解决方案有效:

NgMap.getMap().then(map=>{map.shapes.circle.set('fillColor', '#FF0000')})

Plunker:https://plnkr.co/edit/8m0gU1wqUETR8IjDoqvc?p=preview