Typescript 方法在Angular2中被无序调用。如何正确订阅?
我在Angular2中使用打字脚本。我已订阅http请求的响应,代码如下:Typescript 方法在Angular2中被无序调用。如何正确订阅?,typescript,angular,Typescript,Angular,我在Angular2中使用打字脚本。我已订阅http请求的响应,代码如下: search(searchTerm): void { this._webservice.getSearchResult(searchTerm).subscribe(results => this.results = results); this.callThisMethod(); } 我要做的是更新this.results的值,然后在变量更新后调用this.callThisMethod()this.c
search(searchTerm): void {
this._webservice.getSearchResult(searchTerm).subscribe(results => this.results = results);
this.callThisMethod();
}
我要做的是更新this.results的值,然后在变量更新后调用
this.callThisMethod()
this.callThisMethod()
被调用的顺序不正确。如何在更新变量后运行该方法?一旦响应可用,将异步调用可观察的订阅,而不是同步调用。如果您希望仅在那时调用this.callThisMethod()
,则需要将其作为异步回调的一部分:
search(searchTerm): void {
this._webservice.getSearchResult(searchTerm)
.subscribe(results => {
this.results = results;
this.callThisMethod();
});
}