我有以下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