【发布时间】:2016-04-10 02:50:05
【问题描述】:
我正在尝试编写一个使用 ES6 和 Angular 调用 API 的快速测试——但我无法完全弄清楚如何将注入的值限定在回调中——在所有情况下,它们似乎都是完全隔离的。有什么建议吗??
class Test {
constructor($state) {
this.$state = $state;
}
doThing() {
var request = this.$http({
method: "get",
url: "https://blah,
params: {
action: "get"
}
});
return ( request.then(this.handleSuccess, this.handleError) );
}
handleSuccess(response) {
update $state...
this.$state is undefined
this is undefined
}
handleError(response) {
update $state...
}
}
Test.$inject = ['$state'];
【问题讨论】:
-
它将是未定义的或窗口,因为您将函数传递到 .then 而不将它们绑定回
this。 -
但是如何将它们绑定回这个? then 的 $http 版本没有绑定方法?
标签: angularjs ecmascript-6 es6-promise angularjs-http