javascript - Angular 6 检测图形变化

标签 javascript angular typescript leaflet ngonchanges

我正在使用 Angular 6 和 Leaflet 进行映射。我想创建一个交互式 map ,根据我在表单中选择的属性更改 View 。

我已经在 ngOninit 中设置了绘制图形的条件,但该条件仅在加载应用程序时有效,而我希望它在每次修改表单时都有效。

有人可以把我放在轨道上吗?

Component.ts

export class GeoComponent implements OnInit, OnChanges {

    searchForm: FormGroup;
    geosToDisplay;
    topping: string;
    toppings = new FormControl( [''];
    toppingList: string[] = [
     'Evaluation de la douleur',
     'Evaluation psychologique'
      ];  

constructor(private geoService: GeoService, private router: Router, private database: AngularFireDatabase,private formBuilder: FormBuilder,private matIconRegistry: MatIconRegistry) { }           

ngOnInit() { 
this.geosToDisplay = this.geoService.getGeos();

this.searchForm.valueChanges.subscribe((value) => {
let val =this.toppings.value;
this.toppings.valueChanges.subscribe(val=>console.log(val));
console.log(val);

const myfrugalmap = L.map('frugalmap').setView([47.482019, -0.554399], 7);

L.tileLayer('http://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png', {attribution: 'SoS Map' }).addTo(myfrugalmap);

const myIcon = L.icon({
 iconUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.2.0/images/marker-icon.png',
 iconSize: [ 25, 41 ],
 iconAnchor: [ 13, 41 ],
});

L.marker([ 47.210804, -1.554723 ], {icon: myIcon}).bindPopup('CHU Nantes').addTo(myfrugalmap);

if (val.includes('Evaluation de la douleur')) 
{ L.marker([ 47.482019, -0.554399  ], {icon: myIcon}).bindPopup('CHU Angers').addTo(myfrugalmap);}
else{}
});    
}
}

Component.HTML

    <mat-form-field  appearance="outline" style="width:100%" >
      <mat-label>Rechercher un soin de support</mat-label>
      <mat-select placeholder="Toppings" [formControl]="toppings" multiple Z>
        <button 
                  mat-raised-button 
                  class="mat-primary fill text-sm" 
                  (click)="selectAll()">
                  Tout sélectionner
                </button>&nbsp;&nbsp;
                <button 
                  mat-raised-button 
                  class="mat-warn fill text-sm" 
                  (click)="deselectAll()">
                  Tout désélectionner
                </button>
        <mat-option *ngFor="let topping of toppingList" [value]="topping" >{{topping}}</mat-option>
      </mat-select>
    </mat-form-field>
    <div id="frugalmap">

    </div>

最佳答案

你应该能够做到这一点:

this.someFormGroup.valueChanges.subscribe((value) => {
  // Do something
});

这假设您已经创建了一个 FormGroup 并向其添加了 FormControl。您已将这些导入到您的组件文件中,所以我认为您会使用它们。

Reactive Forms

Form Group

关于javascript - Angular 6 检测图形变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52166937/

相关文章:

angular - 如何在 ionic 选择上获得多个值

javascript - 如何正确地为酒吧滑动制作动画?

javascript - Backbone.js 如何跟踪登录用户状态以及有关应用程序结构的一般建议

Angular 2 + rxjs - 如何返回通过多个后续 http 请求获取的对象流

javascript - 如何从 Angular 发送 2 个数字,用 Node.js 将它们相加并在 Angular 中显示结果? (只有那两个......没有数据库,没有图书馆)

angularjs - Angular 2 GET withCredentials(接受来自服务器的cookie)

typescript - 使用 mocha 和 sinon 方法装饰器对函数进行单元测试

javascript - 术语上的区别? JS 范围

javascript - jquery.validate.js 内部错误 "javaScript runtime error: Unable to get property ' 调用'未定义或空引用“

angular - Visual Studio 2017 使用 webpack 和 angular 在单独的项目中共享 typescript 文件