簡體   English   中英

Angular4“更改”監聽器僅觸發一次

[英]Angular4 “change” listener only fires once

我有一個選擇下拉列表,我正在根據選擇從更改中繪制數據。 它將在第一次加載時正常運行,但是在我重新加載或重定向之前不會再次更改。

    getLogs(){
  document.getElementById("placeSelect").addEventListener('change', (e:any)=>{
    this.placeService.getPlace(e.target.value).subscribe((e:any)=>{        
      this.personService.person(e.data.place.person.id).subscribe((e:any) => {
        this.logs = e.data.person.logs
      })
    })
  })

}

我在組件的“ ngOnInit”上調用它,也許就是問題所在? 任何想法都會有所幫助。

這可能對您有幫助。

將此代碼添加到Module.ts的導入中

import { FormsModule } from '@angular/forms';
imports: [FormsModule]

內部component.ts

 myModel(e: Event) {
    console.log(e);
  }

在Component.html中

   <select (ngModelChange)="myModel($event)" [ngModel]="mymodel">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">2</option>
</select>

感謝您的所有建議,但我設法解決了這個問題。 最大的障礙是選擇位於另一個組件中(對此我沒有提及,對此深表歉意),因此我不得不將選擇信息導出到要顯示該信息的組件中。

component2.ts

import { userInfo } from '../../component1';

然后設置一個延遲,以便在兩個組件都完全加載后啟動偵聽。 似乎在我以前的版本中之前

ngOnInit() {
if(userInfo.place){
  this.getLogsfromPlace()
}    
setTimeout(()=>{
  this.startupUserInfo = userInfo
  document.getElementById("header").addEventListener('change', (e:any)=>{
  this._placeService.getPlace(userInfo.place).subscribe((e:any)=>{
    this._personService.person(e.data.place.person.id).subscribe((e:any) => {
      this.logs = e.data.person.logs
    })
  })
})  }, 3000);
}

非常感激!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM