AngularJS Factory 中的 THIS 范围
AngularJS scope of THIS in Factory
我很困惑因为'this' 属性.
“delete this.user;
”在 AuthenticationFactory
中是什么意思。我认为函数“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");
}
}
}
});
如果再往下看,控制器代码:
$scope.login = function() {
var username = $scope.user.username,
password = $scope.user.password;
if (username !== undefined && password !== undefined) {
UserAuthFactory.login(username, password).success(function(data) {
AuthenticationFactory.isLogged = true;
AuthenticationFactory.user = data.user.username;
AuthenticationFactory.userRole = data.user.role;
$window.sessionStorage.token = data.token;
$window.sessionStorage.user = data.user.username; // to fetch the user details on refresh
$window.sessionStorage.userRole = data.user.role; // to fetch the user details on refresh
$location.path("/");
}).error(function(status) {
alert('Oops something went wrong!');
});
} else {
alert('Invalid credentials');
}
};
成功登录后,控制器将属性 user
和 userRole
添加到 AuthenticationFactory。
我很困惑因为'this' 属性.
“delete this.user;
”在 AuthenticationFactory
中是什么意思。我认为函数“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");
}
}
}
});
如果再往下看,控制器代码:
$scope.login = function() {
var username = $scope.user.username,
password = $scope.user.password;
if (username !== undefined && password !== undefined) {
UserAuthFactory.login(username, password).success(function(data) {
AuthenticationFactory.isLogged = true;
AuthenticationFactory.user = data.user.username;
AuthenticationFactory.userRole = data.user.role;
$window.sessionStorage.token = data.token;
$window.sessionStorage.user = data.user.username; // to fetch the user details on refresh
$window.sessionStorage.userRole = data.user.role; // to fetch the user details on refresh
$location.path("/");
}).error(function(status) {
alert('Oops something went wrong!');
});
} else {
alert('Invalid credentials');
}
};
成功登录后,控制器将属性 user
和 userRole
添加到 AuthenticationFactory。