[英]Angular2 ngModel not updated after async callback
为什么angular2双向数据绑定在这种情况下不起作用?
<span style="color:white">{{searchLocation}}</span>
<input name="searchLocation" type="text" placeholder="Search" [(ngModel)]="searchLocation">
<button class="btn btn-outline-success" type="submit" (click)="search()">Search</button>
目标组件是HeaderComponent
export class HeaderComponent implements OnInit {
searchLocation: string;
@Output() locationFound: EventEmitter<Position> = new EventEmitter<Position>();
constructor(private _locationService: LocationService) { }
ngOnInit() {
this.searchLocation ="";
}
search():void{
this._locationService.geocodeAddress(this.searchLocation)
.subscribe((position:Position)=>{
this.searchLocation ="new value";
this.locationFound.emit(position);
});
}
}
在订阅块之后,searchLocation会更改,但视图不会更新。
我希望有人可以帮助我
this.searchLocation ="";
searchLocation被指定为null。 如果存在null,则不会更新DOM。 尝试给一些有效的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.