代码之家  ›  专栏  ›  技术社区  ›  Lauren

属性绑定映射单击事件和下拉列表选择ngx单张/角度2中的输入

  •  1
  • Lauren  · 技术社区  · 6 年前

    我有一张区域多边形地图。在地图下面,我有一个下拉选择输入,它动态地将多边形名称作为选项读取。当用户单击地图上的区域多边形时,我希望下拉列表select input使用选定的多边形名称(在geojson文件中作为“名称”字段存在)进行更新。

    我认为实现这一点的方法是在html模板文件中使用属性绑定。所以在select类中,我将value属性设置为 clicked ,像这样:

    <select class="form-control" id="selectRegion" [value]="clicked">
    

    点击 最初在应用程序组件中定义为空字符串。当单击多边形时,我设置 点击 作为 onEachFeature 单击事件。控制台日志显示 点击 变量正在正确更新。但是,select输入不会像预期的那样用click事件更新。

    我怀疑问题是 this 在我的函数中没有正确更新吗?如何使用我的map click事件更新select输入?

    下面是一些代码(基于Asymmetrik的NGX单张教程NGCLI):

    GeoJSON(另存为 polygons.geojson 在“资源”文件夹中:

    {
      "type": "FeatureCollection",
      "features": [
        {
          "type": "Feature",
          "properties": {
            "stroke": "#555555",
            "stroke-width": 2,
            "stroke-opacity": 1,
            "fill": "#555555",
            "fill-opacity": 0.5,
            "name": "poly1"
          },
          "geometry": {
            "type": "Polygon",
            "coordinates": [
              [
                [
                  -121.95098876953125,
                  46.82966386051541
                ],
                [
                  -121.78482055664061,
                  46.82966386051541
                ],
                [
                  -121.78482055664061,
                  46.91368905872705
                ],
                [
                  -121.95098876953125,
                  46.91368905872705
                ],
                [
                  -121.95098876953125,
                  46.82966386051541
                ]
              ]
            ]
          }
        },
        {
          "type": "Feature",
          "properties": {
            "stroke": "#555555",
            "stroke-width": 2,
            "stroke-opacity": 1,
            "fill": "#555555",
            "fill-opacity": 0.5,
            "name": "poly2"
          },
          "geometry": {
            "type": "Polygon",
            "coordinates": [
              [
                [
                  -121.77726745605469,
                  46.83107318799318
                ],
                [
                  -121.62963867187499,
                  46.83107318799318
                ],
                [
                  -121.62963867187499,
                  46.913220009605624
                ],
                [
                  -121.77726745605469,
                  46.913220009605624
                ],
                [
                  -121.77726745605469,
                  46.83107318799318
                ]
              ]
            ]
          }
        }
      ]
    }
    

    应用模块:

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { LeafletModule } from '@asymmetrik/ngx-leaflet';
    import { HttpClientModule } from '@angular/common/http';
    
    
    import { AppComponent } from './app.component';
    
    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [
        BrowserModule,
        LeafletModule.forRoot(),
        HttpClientModule
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
    

    app.component.html版本:

    <div class="map"
      leaflet
      [leafletLayers]="layers"
         [leafletFitBounds]="fitBounds"></div>
    <div class="form-group">
      <select class="form-control" id="selectRegion" [value] = "clicked">
        <option *ngFor="let region of regions">{{ region }}</option>
      </select>
    </div>
    

    app.component.ts版本:

    import { Component, OnInit } from '@angular/core';
    import { HttpClient } from '@angular/common/http';
    
    import * as L from 'leaflet';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent implements OnInit {
    
      layers: L.Layer[];
      fitBounds = [[46.67, -122.25], [47.01, -121.302]];
      regions = [];
      clicked = '';
    
      constructor(private http: HttpClient) { }
    
      ngOnInit() {
    
        // read in geojson of poly
        this.http.get<any>('/assets/polygons.geojson')
          .subscribe(poly => {
    
            const tileLayer = L.tileLayer('https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_nolabels/{z}/{x}/{y}.png', {
              subdomains: 'abcd',
              maxZoom: 19
            });
    
            const polyLayer = L.geoJSON(poly, {
              onEachFeature: this.onEachFeature.bind(this)
            });
    
            this.layers = [ tileLayer, polyLayer ];
            console.log(this);
          });
      }
    
      // loop through each feature of polyLayer
      onEachFeature(feature, layer) {
    
        // push polygon names to regions array
        this.regions.push(feature.properties.name);
    
        layer.on('click', <LeafletMouseEvent> (e) => {
          this.clicked = e.target.feature.properties.name;
          console.log(this.clicked);
        });
      }
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   reblace    6 年前

    这个 onEachFeature 功能与 layer.on('click'... 事件回调可能在角度区域之外调用。如果是这样,那么更改检测将不会自动工作。你应该把变化包装在 zone.run() 调用以确保在角度区域中进行更改-如本例中所示:

    // loop through each feature of polyLayer
    onEachFeature(feature, layer) {
    
      this.zone.run(() => {
        // push polygon names to regions array
        this.regions.push(feature.properties.name);
    
        layer.on('click', <LeafletMouseEvent> (e) => {
          this.zone.run(() => {
            this.clicked = e.target.feature.properties.name;
            console.log(this.clicked);
          }
        });
      }
    
    }
    

    你可以遵循这些指示( https://github.com/Asymmetrik/ngx-leaflet#a-note-about-change-detection )查看其工作原理并将ngzone实例注入组件。

    推荐文章