【问题标题】:OpenLayers3 Using an Angular class property in Style FunctionOpenLayers3 在样式函数中使用 Angular 类属性
【发布时间】:2017-07-19 16:53:50
【问题描述】:

我创建了一个 Angular2/4 应用程序,它使用 OpenLayers3 为沙盒游戏 Wur​​m Online 渲染岛屿地图。它工作得非常漂亮,取代了我用纯 JS 创建的旧版本。

当前演示:http://www.wurmonlinemaps.com/maps/xanadubeta

代码仓库:https://github.com/WefNET/wurmonlinemaps-ng

我想为最终用户提供自定义屏幕上呈现的某些功能的颜色的能力。最后,我想使用 localStorage 概念来保存用户偏好。

我希望不起作用的方法是:将矢量图层的 stylefuncton 中的样式属性设置为 Angular 类属性值。

基本概念

在这个伪代码示例中,“deedColor”Angular 类属性设置为一个值,然后我尝试在样式函数中使用它:

export class AppComponent {
    deedColor: string = "rgba(255,0,0,0.4)";

    var deedStyleFunction = function (feature, resolution) {
        return [
        new ol.style.Style({
          image: new ol.style.RegularShape({
              points: 4,
              radius: 11 / resolution,
              angle: Math.PI / 4,
              fill: new ol.style.Fill({
                  color: this.deedColor
           }),
        })
      ]
    }

    // hundreds of other lines
}

遗憾的是,样式函数无法识别 Angular 类属性:

ERROR TypeError: Cannot read property 'deedColor' of undefined

经过一些实验,我似乎无法从 OL StyleFunction 中访问任何 Angular 对象。

有什么想法吗?

【问题讨论】:

    标签: angular openlayers-3


    【解决方案1】:

    这可能是范围问题 - 尝试将您的函数绑定到 ng 对象:

    deedStyleFunction = function(feature, resolution) {
    
    }.bind(this);
    

    【讨论】:

      猜你喜欢
      • 2019-03-14
      • 1970-01-01
      • 1970-01-01
      • 2015-01-02
      • 1970-01-01
      • 2014-02-17
      • 1970-01-01
      • 2017-12-14
      • 2015-10-14
      相关资源
      最近更新 更多