场景:我为用户登录时为我的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: '/'
});
}]);
你可以发布你的路由吗? – Chrillewoodz
@Chrillewoodz一会儿就好了。 –