异步回调后Angular2 ngModel未更新

为什么在这种情况下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变化但视图不被更新。

我希望有人能帮助我

回答:

 this.searchLocation =""; 

searchLocation被分配到空。如果存在null,它将不会更新DOM。尝试给一些有效的字符串。

以上是 异步回调后Angular2 ngModel未更新 的全部内容, 来源链接: utcz.com/qa/257794.html

回到顶部