【问题标题】:Angular rootScope works then goes undefined on refresh - AngularFireAngular rootScope 工作,然后在刷新时未定义 - AngularFire
【发布时间】:2015-09-30 04:51:59
【问题描述】:

场景:我在登录时为用户分配了一些值到我的rootScope。现在我希望能够使用这些值,以便每次用户发布会议时,都应该将其添加到 Firebase db 中的信息下。

问题:我做得很好,当我登录时,我从用户那里发布了会议。但是一旦页面刷新,rootScope.currentUser 就变得未定义。如何防止 rootScope.currentUser 未定义?我的控制器和我的工厂如下:

我的控制器:

myApp.controller('MeetingsController', function($scope, $firebaseObject, $firebaseArray, $rootScope, FIREBASE_URL, SomeURL){

    //rootScope.currentUser.$id works the first time i post then the second it doesn't
    var ref = new Firebase(FIREBASE_URL + $rootScope.currentUser.$id + SomeURL);
    var meetings = $firebaseObject(ref);
    $scope.meetings = meetings;

    $scope.addMeeting = function(){
        ref.push({
          name: $scope.meetingname,
          date: Firebase.ServerValue.TIMESTAMP
       });
    };
});//controller for around me

我的工厂:

myApp.factory('Authentification', function($firebase, $rootScope, $firebaseObject, $firebaseAuth, $routeParams, $location, FIREBASE_URL){   

    var ref = new Firebase(FIREBASE_URL);
    var auth = $firebaseAuth(ref);
    auth.$onAuth(function(authUser){
        if(authUser){
            var firebaseUsers = new Firebase(FIREBASE_URL+'/users/'+authUser.uid);
            var  user       = $firebaseObject(firebaseUsers);
            $rootScope.currentUser = user;
        } else {
            $rootScope.currentUser = '';
        }
    });
    var myObject = {
        login: function(user){
            return auth.$authWithPassword({
                email: user.email,
                password: user.pswd
            });
        }, 
        logout: function(user){
            return auth.$unauth();
        },
        requireAuth: function() {
            return auth.$requireAuth();
        }
    };
    return myObject;
});

路线:

myApp.config( ['$routeProvider', function($routeProvider){
    $routeProvider.
        when('/login', {
            templateUrl: 'views/login.html', 
            controller: 'RegistrationController' 
        }).
        when('/register',{
            templateUrl: 'views/register.html',
            controller: 'RegistrationController' 
        }).
        when('/aroundme', {
            templateUrl: 'views/aroundme.html' ,
            controller: 'MeetingsController', 
            resolve: {
                currentAuth: function(Authentification){
                    return Authentification.requireAuth();
                }
            }
        }).
        otherwise({
            redirectTo: '/'
        });
}]);

【问题讨论】:

  • 你能发布你的路线吗?
  • @Chrillewoodz 等一下。

标签: angularjs angularjs-scope firebase angularfire


【解决方案1】:

问题可能是您在完成加载之前将$firebaseObject 分配给$rootScope。要确保它在绑定到$rootScope 之前已经加载,请使用$loaded()

auth.$onAuth(function(authUser){
    if(authUser){
        var firebaseUsers = new Firebase(FIREBASE_URL+'/users/'+authUser.uid);
        var  user       = $firebaseObject(firebaseUsers);

        user.$loaded()
          .then(function(data) {
            $rootScope.currentUser = data;
          })
          .catch(function(err) {
            // Handle error
        });
    } else {
        $rootScope.currentUser = '';
    }
});

来自the docs

请注意,数据不会立即可用,因为检索它是一个异步操作。您可以使用 $loaded() 承诺在数据加载时收到通知。

【讨论】:

  • 同样的问题。我的信念是:问题来自 rootScope。第一次有效,第二次无效。我必须注销并重新登录。它似乎没有保留该值。
  • 如果将其设置为常规范围会发生什么?这就是我拥有它的方式,然后我在 mainctrl 中进行了 $onAuth 调用,并将其设置为 html 标记上的属性以始终可用。
猜你喜欢
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-22
  • 2020-02-21
  • 2019-01-21
  • 2020-06-07
相关资源
最近更新 更多