2015-07-11 33 views
1

场景:我为用户登录时为我的rootScope分配了一些值。现在我希望能够使用这些值,以便每次用户发布会议时,都应将其添加到Firebase数据库的信息中。角度rootScope作品然后在刷新时未定义 - AngularFire

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

你可以发布你的路由吗? – Chrillewoodz

+0

@Chrillewoodz一会儿就好了。 –

回答

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()承诺在数据加载时获得通知。

+1

同一问题。我的信念是:问题来自rootScope。它第一次工作,然后第二次不工作。我必须注销并重新登录。似乎没有保留该值。 –

+0

如果将其设置为常规范围,会发生什么情况?这就是我的方式,然后我在mainctrl中创建了$ onAuth调用,并将其设置为html标签上的一个属性,以便始终可用。 – Chrillewoodz