gpt4 book ai didi

异步回调后 Angular2 ngModel 未更新

转载 作者:太空狗 更新时间:2023-10-29 18:26:20 26 4
gpt4 key购买 nike

为什么 angular2 双向数据绑定(bind)在这种情况下不起作用?

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

订阅 block 后,searchLocation 发生变化,但 View 未更新。

我希望有人能帮助我

最佳答案

      this.searchLocation ="";

searchLocation is assigned to null. If null is present it won't update the DOM. Try giving some valid string.

关于异步回调后 Angular2 ngModel 未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40357850/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com