简体   繁体   English

Angular2 ngModel在异步回调后未更新

[英]Angular2 ngModel not updated after async callback

Why angular2 two way data binding doesn' t work in this scenario ? 为什么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>

The target component is HeaderComponent 目标组件是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);
    });
  }
}

After the subscribe block the searchLocation change but the view is not updated. 在订阅块之后,searchLocation会更改,但视图不会更新。

I hope somebody can help me 我希望有人可以帮助我

      this.searchLocation ="";

searchLocation is assigned to null. searchLocation被指定为null。 If null is present it won't update the DOM. 如果存在null,则不会更新DOM。 Try giving some valid string. 尝试给一些有效的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM