【问题标题】:How to make a variable visible within the scope of a function如何使变量在函数范围内可见
【发布时间】:2021-11-22 07:09:50
【问题描述】:

如下面的代码所示,我声明了变量AoC,我想给它赋值feature.get(environment.KEY_OF_MVT_FEATURE_1)。我面临的问题是变量AoC 未在函数VectorTileLayer 的范围内定义 .请告诉我如何在VectorTileLayer范围内访问变量AoC

代码

AoC //declaration
public visualisePolygonsAsMVTTilesOnMapWithColorsForAverageHeightsForZone(zoneToken){
return new VectorTileLayer({
        source: new VectorTileSource({
            format: new MVT(),
            url: environment.LocalHostForVectorTileSourceAsMVTTileForZXYWS + zoneToken + "/{z}/{x}/{y}",
        }),
        opacity: .4,
        style: function (feature){
            console.log("MVTTileAsFeature:",feature)
            this.test = feature
            let fillColor
            let strokeColor
            let text = ""
            this.AoC = feature.get(environment.KEY_OF_MVT_FEATURE_1)//<===============
        if (feature.get(environment.KEY_OF_MVT_FEATURE_IS_TREATMENT) == true) {
        if (feature.get(environment.KEY_OF_MVT_FEATURE_1) >= 0 && feature.get(environment.KEY_OF_MVT_FEATURE_1) <= 12.5){
            text = feature.get(environment.KEY_OF_MVT_FEATURE_1) + "%"
            fillColor = '#ff0000'
            strokeColor = '#000000'
        } else if (feature.get(environment.KEY_OF_MVT_FEATURE_1) > 12.5 && feature.get(environment.KEY_OF_MVT_FEATURE_1) <= 15) {
            text = feature.get(environment.KEY_OF_MVT_FEATURE_1) + "%"
            fillColor = '#fd4900'
            strokeColor = '#000000'
        } else if (feature.get(environment.KEY_OF_MVT_FEATURE_1) > 15 && feature.get(environment.KEY_OF_MVT_FEATURE_1) <= 27.5) {
            text = feature.get(environment.KEY_OF_MVT_FEATURE_1) + "%"
            fillColor = '#f66d00'
            strokeColor = '#000000'
        } else if (feature.get(environment.KEY_OF_MVT_FEATURE_1) > 27.5 && feature.get(environment.KEY_OF_MVT_FEATURE_1) <= 40) {
            text = feature.get(environment.KEY_OF_MVT_FEATURE_1) + "%"
            fillColor = '#e98b00'
            strokeColor = '#000000'
            }
        }
         return new Style({
                fill: new Fill({
                    color: fillColor
                  }),
                stroke: new Stroke({
                    color: strokeColor,
                    lineDash: [1],
                    width:1,
                  }),
                  text: new Text({
                    font: 'Normal 9px Arial',
                    text: ''+text,
                    fill: new Fill({
                      color: '#000000'
                    }),
                    stroke: new Stroke({
                      color: '#000000',
                      width: 0
                    }),
                    // offsetX: -45,
                    // offsetY: 0,
                    // rotation: 0
                  })
                })
        }
      });

【问题讨论】:

    标签: angular typescript openlayers angular-openlayers


    【解决方案1】:

    多种方式:

    1. 您可以创建一个angular service,在其中添加 AoC 并使用getter/setter 公开它。 或
    2. 如果是AngularJs 代码,您也可以将其添加到$rootScope,以便您可以在任何地方访问它。

    【讨论】:

    • 我试过了,也没有解决问题
    猜你喜欢
    • 1970-01-01
    • 2012-06-06
    • 2014-04-05
    • 2017-10-07
    • 1970-01-01
    • 2012-10-04
    • 2016-10-26
    • 1970-01-01
    • 2013-03-12
    相关资源
    最近更新 更多