[英]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.