Angular4 "change" 侦听器仅触发一次
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>
感谢所有建议,但我设法解决了。最大的障碍是 select 在不同的组件中(我为未能提及而道歉)所以我不得不将 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);
}
非常感谢!
我有一个选择下拉列表,我正在根据选择从中提取数据。它会在第一次加载时正常运行,但在我重新加载或重定向之前不会再次更改。
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>
感谢所有建议,但我设法解决了。最大的障碍是 select 在不同的组件中(我为未能提及而道歉)所以我不得不将 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);
}
非常感谢!