【发布时间】:2016-08-30 14:36:38
【问题描述】:
考虑下面的类
class LoginController{
constructor(authService,$timeout,$state){
let vm = this;
this.loading = false;
this._authService = authService;
this._$timeout = $timeout;
this._$state = $state;
this.loading = false;
this.statusMessage = null;
}
login(){
this.loading = true;
this.statusMessage = null;
let loginModel = {
UserName : this.username,
Password : this.password,
RememberMe : this.rememberMe
};
//Login User
this._authService.login(loginModel).then(function(user){
//Set User Login & send to Dashboard
this._authService.setUser(user);
this._$state.go("dashboard");
}, function(error){
const errorMessage = error ? error.Message : "Undefined Login Issue occurred !";
this.loading = false;
});
}
}
一切正常,除了我点击了错误回调函数,它到达this.loading = false;,由于某种原因,这是未定义的。
如何在错误回调中保留对“this”类的引用?
【问题讨论】:
-
您是否已经解决了问题或需要更多帮助?
标签: javascript angularjs class ecmascript-6 webpack