【发布时间】:2015-06-30 09:34:56
【问题描述】:
因为 'this' 属性,我很困惑。
AuthenticationFactory 中的“delete this.user;”是什么意思。我认为函数“check”是一种方法,因此它将与“auth”对象绑定。但是,“auth”对象中没有“user”属性。能解释一下吗?
另外,在“UserAuthFactory”中(delete AuthenticationFactory.user,delete AuthenticationFactory.userRole)
我不知道什么是“user”和“userRole”属性。 AuthenticationFactory 中没有此类属性。
这是来自http://thejackalofjavascript.com/architecting-a-restful-node-js-app/的我的代码
myApp.factory('AuthenticationFactory', function($window) {
var auth = {
isLogged: false,
check: function() {
if ($window.sessionStorage.token && $window.sessionStorage.user) {
this.isLogged = true;
} else {
this.isLogged = false;
delete this.user;
}
}
}
return auth;
});
myApp.factory('UserAuthFactory', function($window, $location, $http, AuthenticationFactory) {
return {
login: function(username, password) {
return $http.post('http://localhost:3000/login', {
username: username,
password: password
});
},
logout: function() {
if (AuthenticationFactory.isLogged) {
AuthenticationFactory.isLogged = false;
delete AuthenticationFactory.user;
delete AuthenticationFactory.userRole;
delete $window.sessionStorage.token;
delete $window.sessionStorage.user;
delete $window.sessionStorage.userRole;
$location.path("/login");
}
}
}
});
【问题讨论】:
标签: javascript angularjs this factory