加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – $rootScope.currentUser刷新时为空

发布时间:2020-12-17 07:43:11 所属栏目:安全 来源:网络整理
导读:我能够使firebaseSimpleLogin工作并将root用户存储在rootScope中.当我转到另一个页面并返回到feed页面时,所有的东西都会加载,但是当我刷新$rootScope.currentUser为null时.其他人遇到过这个问题吗? 我的app.run函数中有这段代码: $rootScope.$on('$firebas
我能够使firebaseSimpleLogin工作并将root用户存储在rootScope中.当我转到另一个页面并返回到feed页面时,所有的东西都会加载,但是当我刷新$rootScope.currentUser为null时.其他人遇到过这个问题吗?

我的app.run函数中有这段代码:

$rootScope.$on('$firebaseSimpleLogin:login',function(e,auth){
   $rootScope.currentUser = User.find(auth.id);
});

这是我的FeedCtrl,试图加载用户的帖子

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.

为了保持值,您需要将它们存储在cookie中或使用localStorage / sessionStorage.例如,而不是使用$rootscope.currentUser使用$window.sessionStorage.currentUser.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读