angular.js - angular登錄完成后,如何正確在header顯示用戶數(shù)據(jù)
問題描述
如圖所示:右上角紅框內(nèi)是登錄成功后,用戶名顯示的位置
我現(xiàn)在是通過angular事件傳遞來做的。每次手動刷新頁面后,用戶的數(shù)據(jù)就取不到了。這是應(yīng)該用那種方式來做比較好?求指教!
LoginController:登錄視圖控制器
function LoginController($rootScope, $scope, $state, $window, AUTH_EVENTS, AuthService, ProfileService) { var vm = this; vm.login = login;function login() { var userInfo = {username: vm.username,password: vm.password }; AuthService.login(userInfo).then(function () {ProfileService.fetchUserInfo().then(function (res) { $scope.$emit(AUTH_EVENTS.loginSuccess, res); $state.go(’app.program.list’);}) }) } }
AppCtrl:系統(tǒng)框架控制器,包含header
function AppCtrl($rootScope, $scope, $state, $translate, $localStorage, $window, $document, $timeout, cfpLoadingBar, AuthService, AUTH_EVENTS) { $rootScope.$on(AUTH_EVENTS.loginSuccess, function (e,data) { $rootScope.user = data.data; }); })
問題解答
回答1:angular.module('myApp',[]).run(function (AuthService) { AuthService.login();})
