ES6 中 $http 回调的作用域

Scope of $http callback in ES6

我正在尝试编写一个使用 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'];

来自 Felix 的优秀 link 将我带到了我需要去的地方 - 不是 100% 确信这是最好的方式,但它至少相当干净。 HandleSuccess 和 HandleError 也更改为接受可选范围。

    return ( request.then((response)=> {
        this.handleSuccess(response, this)
    }).catch((response)=> {
        this.handleError(response, this)
    }));