3
爲什麼在這種情況下angular2雙向數據綁定不起作用?異步回調後Angular2 ngModel未更新
<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變化但視圖不被更新。
我希望有人能幫助我
我不知何故錯過了'searchLocation'應該更新的部分。我只看到'this.searchLocation =「」;'兩次,這並不是什麼大的改變;-)。 –