我想获取有关当前登录用户的一些信息,我已将该变量存储在currentUser
中,但是当我尝试从控制器访问它时什么都没显示
authentication.service.js:
(function () {
'use strict';
angular
.module('thinkster.authentication.services')
.factory('Authentication', Authentication);
Authentication.$inject = ['$cookies', '$http'];
/**
* @namespace Authentication
* @returns {Factory}
*/
function Authentication($cookies, $http) {
/**
* @name Authentication
* @desc The Factory to be returned
*/
var Authentication = {
login: login,
register: register,
getAuthenticatedAccount: getAuthenticatedAccount,
isAuthenticated: isAuthenticated,
setAuthenticatedAccount: setAuthenticatedAccount,
unauthenticate: unauthenticate,
currentUser: ''
};
return Authentication;
////////////////////
/**
* @name register
* @desc Try to register a new user
* @param {string} username The username entered by the user
* @param {string} password The password entered by the user
* @param {string} email The email entered by the user
* @returns {Promise}
* @memberOf thinkster.authentication.services.Authentication
*/
function register(email, password, username) {
return $http.post('/api/v1/accounts/', {
username: username,
password: password,
email: email,
}).then(registerSuccessFn, registerErrorFn);
function registerSuccessFn(data ̶,̶ ̶s̶t̶a̶t̶u̶s̶,̶ ̶h̶e̶a̶d̶e̶r̶s̶,̶ ̶c̶o̶n̶f̶i̶g̶ ) {
Authentication.login(email, password)
};
function registerErrorFn(data ̶,̶ ̶s̶t̶a̶t̶u̶s̶,̶ ̶h̶e̶a̶d̶e̶r̶s̶,̶ ̶c̶o̶n̶f̶i̶g̶,) {
console.log(data)
}
}
function login(email, password){
return $http.post('/api/v1/auth/login/', {
email: email,
password: password
}).then(loginSuccessFn, loginErrorFn);
function loginSuccessFn(data ̶,̶ ̶s̶t̶a̶t̶u̶s̶,̶ ̶h̶e̶a̶d̶e̶r̶s̶,̶ ̶c̶o̶n̶f̶i̶g̶ ) {
Authentication.setAuthenticatedAccount(data.data);
// pass data through to dashboard controller
window.location = '/dashboard'
Authentication.currentUser += data.config.data.email
console.log(Authentication.currentUser)
}
function loginErrorFn(data, status, headers, config) {
console.error('Failed');
console.log(data)
}
}})();
dashboard.controller.js
//仪表板控制器
(function() {
'use strict';
angular
.module('thinkster.authentication.controllers')
.controller('DashboardController', DashboardController);
DashboardController.inject = ['$location, $scope', 'Authentication'];
function DashboardController($location, $scope, Authentication){
var vm = this;
vm.user_info = user_info
//vm.user_info2 = user_info
function user_info() {
return Authentication.currentUser
}
}
})();
我正在尝试更新currentUser
中的loginSuccessFn()
,该服务中的更新成功。我可以在控制台中看到它,但是当我尝试在控制器中引用它时却看不到。预先感谢
答案 0 :(得分:1)
首先,为什么要使用+ =
设置变量({Authentication.currentUser += data.config.data.email
)
使用=设置当前用户
Authentication.currentUser = data.config.data
Authentication.currentUser = data.config.data.email
第二个user_info是一个函数,因此您必须调用它,更改
vm.user_info = user_info
至vm.user_info = user_info()
我也将camelCase用于函数,将under_score用于对象字段,以提高可读性和一致性
示例
function userInfo() { // camelCase
return Authentication.currentUser;
}
var vm = this
vm.user_info = userInfo() // under_score object