我有以下UserService
其中我只是在实例中具有一个属性,该属性确定用户isAuthenticated
。出于某种原因,即使通过将this.isAuthenticated
设置为各种值,logout
和其他一些方法来更改值,但Angular并未捕获该更改。我甚至试过做手册$digest
和$apply
,但仍然没有运气。服务中的属性更改不触发更改
export default class UserService {
constructor($http, $state, AppConstants, JWTService) {
'ngInject';
this.$http = $http;
this.$state = $state;
this.isAuthenticated = false;
}
logout() {
this.isAuthenticated = false;
this.$state.go('login', null, { reload: true });
}
verify() {
return new Promise((resolve) => {
// if a user exists, then resolve
if (this.isAuthenticated) {
return resolve(true);
}
this.$http({
method: 'POST',
url: `${this.AppConstants.api}/verify`
})
.then(() => {
this.isAuthenticated = true;
return resolve(true);
})
.catch(() => {
this.isAuthenticated = false;
return resolve(false);
});
});
}
}
代码的工作,当我登录的第一次,我张贴设置this.isAuthenticated
为true,将this.verify
作品。但是,当我logout
,即使this.isAuthenticatd
如此到false
,条件if (this.isAuthenticated)
仍然是true
时,再次调用this.verify
。
这似乎是一个角v1的问题吗?如果是这样,请更新您的代码。 'angular'用于Angular v2 +,'angularjs'用于角度1.谢谢! – DeborahK
更新了标签。 – Detuned