【发布时间】:2016-05-14 02:46:51
【问题描述】:
我是否正在尝试使用 Typescript 将异步服务调用的返回值分配给局部变量;
private searchResult;
public search():void{
this.DashboardService.dashboardSearch(this.searchParams)
.then(
function (result:ISearchResult) {
// promise was fullfilled
console.log(result);
this.searchResult = result;
},
function (error) {
// handle errors here
console.log(error);
}
);
};
<div id="dashboard-content" ng-controller="dashboardCtrl as vm">
<h3>{{vm.searchResult}}</h3>
</div>
dashboardSearch 服务正确返回带有数据的对象。但我无法将数据分配给我的局部变量。
这是来自 Google Chrome console.log 的错误 + 数据
如何将服务中的数据绑定到本地类变量?
【问题讨论】:
-
它与
this关键字有关。之前有一个关于 SO 的答案很好地说明了this如何工作以及如何始终引用正确的this实例。 stackoverflow.com/a/20279485/1260204
标签: javascript asynchronous typescript