我能够使firebaseSimpleLogin工作并将root用户存储在rootScope中.当我转到另一个页面并返回到Feed页面时,所有的东西都会加载,但是当我刷新$rootScope.currentUser为null时.其他人遇到过这个问题吗?
$rootScope.$on('$firebaseSimpleLogin:login',function(e,auth){ $rootScope.currentUser = User.find(auth.id); });
app.controller('FeedCtrl',['$scope','$rootScope','User','Auth','Post',function($scope,$rootScope,User,Auth,Post){ populateFeed(); console.log($rootScope.currentUser); $scope.logout = function(){ Auth.logout(); }; $scope.savePost = function(){ Post.create($scope.post).then(function(post){ $scope.post.text = ""; }); }; function populateFeed(){ $scope.posts = {}; angular.forEach($rootScope.currentUser.posts,function(id){ $scope.posts[id] = Post.find(id); }); } }]);
我的主应用程序模块
var app = angular.module('myapp',['ui.router','firebase']); app.config(function($stateProvider,$urlRouterProvider){ $stateProvider .state('/',{ url: '/',controller: 'MainCtrl',templateUrl: 'views/index.html' }) .state('Feed',{ url: '/Feed',controller: 'FeedCtrl',templateUrl: 'views/Feed.html',authenticate: true }) .state('login',{ url: '/login',controller: 'LoginCtrl',templateUrl: 'views/login.html' }) .state('signup',{ url: '/signup',templateUrl: 'views/signup.html' }) .state('settings',{ url: '/settings',controller: 'SettingsCtrl',templateUrl:"views/settings.html" }) .state('profile',{ url: '/:slug',controller: 'ProfileCtrl',templateUrl: 'views/profile.html' }) .state('profile-about',{ url: '/:slug/about',templateUrl: 'views/profile.html' }); $urlRouterProvider.otherwise('/'); }) .constant('FIREBASE_URL','https://{FIREBASE}.firebaseio.com/') .run(function($rootScope,$state,FIREBASE_URL,$firebaseSimpleLogin,Auth){ $rootScope.$on('$stateChangeStart',function(event,next){ if(next.authenticate && !User.getCurrentUser()){ $state.go('login'); } }); $rootScope.$on('$firebaseSimpleLogin:login',auth){ $rootScope.currentUser = User.find(auth.id); }); $rootScope.$on('$firebaseSimpleLogin:logout',function(){ delete $rootScope.currentUser; $state.go('login'); }); $rootScope.logout = function(){ Auth.logout(); }; });
如果通过“刷新”表示您在浏览器中点击F5,将从内存中擦除您的$rootscope,并且根据您的体验(将基本上整个应用程序“重新启动”)将返回null.
原文链接:https://www.f2er.com/angularjs/142897.html为了保持值,您需要将它们存储在cookie中或使用localStorage / sessionStorage.例如,而不是使用$rootscope.currentUser使用$window.sessionStorage.currentUser.