简体   繁体   English

Angular4“更改”监听器仅触发一次

[英]Angular4 “change” listener only fires once

I have a selection dropdown that I'm drawing data from that changes based on the selection. 我有一个选择下拉列表,我正在根据选择从更改中绘制数据。 It will fire fine on first load, but wont change again until I reload or redirect. 它将在第一次加载时正常运行,但是在我重新加载或重定向之前不会再次更改。

    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
      })
    })
  })

}

I am calling this on the component's "ngOnInit", maybe that's the issue? 我在组件的“ ngOnInit”上调用它,也许就是问题所在? Any ideas would be helpful. 任何想法都会有所帮助。

This may helps you. 这可能对您有帮助。

Add this code in imports of Module.ts 将此代码添加到Module.ts的导入中

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

Inside component.ts 内部component.ts

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

In Component.html 在Component.html中

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

Thanks for all the advice, but I managed a work around. 感谢您的所有建议,但我设法解决了这个问题。 The biggest hurdle is that the select was in a different component (which I apologize for failing to mention) so I had to export the select information to my component where the info would be displayed. 最大的障碍是选择位于另一个组件中(对此我没有提及,对此深表歉意),因此我不得不将选择信息导出到要显示该信息的组件中。

component2.ts component2.ts

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

And then a set a delay so that it would initiate the listen after both components are fully loaded. 然后设置一个延迟,以便在两个组件都完全加载后启动侦听。 It seems it was firing before in my previous version 似乎在我以前的版本中之前

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);
}

Much appreciated! 非常感激!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM