然后Angular rootScope工作在刷新时未定义 - AngularFire

时间:2015-07-11 13:33:25

标签: angularjs angularjs-scope firebase angularfire

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

问题:我这样做很好,当我登录时,我发布了用户的会议。但是一旦页面刷新,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: '/'
        });
}]);

1 个答案:

答案 0 :(得分: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()承诺在数据加载时收到通知。