0
我有這樣的服務:AngularJS服務不設置數據正確
myApp.factory('Auth', function() {
var user;
return {
setUser: function(aUser) {
user = aUser;
},
isLoggedIn: function() {
return (user) ? user : false;
}
}
});
而且我將其注入我的控制器 「loginCtrl」,並設置用戶:
myApp.controller('loginCtrl', ['$scope', '$http', '$state', 'Auth',
function($scope, $http, $state, Auth) {
$scope.login = function() {
$http.post('/login', $scope.local).success(function(response) {
Auth.setUser($scope.local);
console.log(Auth);
console.log(Auth.user);
$state.go('news');
});
};
}
]);
Auth.setUser從未像控制檯在返回「Undefined」之後立即記錄一樣工作。我究竟做錯了什麼?謝謝!
嘗試在工廠編寫訪問(的getUser)函數返回的用戶變量,而不是Auth.user – jmw5598