这是一个控制器,我从firebase获取一些数据并更新我的范围,然后我想在更新范围后做更多的事情:
.controller('loginCtrl', ['$scope','$firebaseAuth','$firebaseObject','$location',
'localStorageService','$q',
function($scope,$firebaseAuth,$firebaseObject,$location,
localStorageService,$q) {
// Called
$scope.googleAuth = function(){
var auth = $firebaseAuth();
auth.$signInWithPopup("google").then(function(firebaseUser){
// Google approved
// Step 1. Get user profile
getProfile(firebaseUser);
}).catch(function(error){
console.log("Authentication failed:",error);
$scope.errorMsg = "Something went wrong. Please try again."
});
}
// Watchers
$scope.$on('profile',function(){
console.info("REACH");
if($scope.profile == null){ // User doesn't exist
console.info("profile null");
if(isAssigned(firebaseUser) != null){
// Redirect to create a profile page
} else {
// Do not let them in TODO: adjust the text
$scope.erorrMsg = "It seems you don't have a profile. Please contact us on ----------- to get a profile";
}
} else {
// Step 2. Get user parent info
// getParent($scope.profile.current_centre_id,$scope.profile.refer_id);
console.info("Getting parent");
getParent('-KeVj_sr-uCY3zx0kbb6','-KeVpUA4OIK4msNIl0gQ');
}
});
// After getting parent info
$scope.$on('parent',function(){
localStorageService.set('currentUser',{
profile: $scope.profile,
parent: $scope.parent
});
});
var getProfile = function(firebaseUser){
var email = firebaseUser.user.email;
var ref = firebase.database().ref();
// Getting profile
$scope.profile = $firebaseObject(ref.child('profiles').orderByChild('email').equalTo('dev.beezbutt@gmail.com'));
}
var getParent = function(centre_id,parent_id){
$scope.parent = $firebaseObject(ref.child('parents/'+centre_id+'/'+parent_id));
}
var isAssigned = function(firebaseUser){
// TODO
return false;
}
}]);
$scope.$on('profile')
& $scope.$on('parent')
没有被触发,我不确定为什么或我错过了什么。
我知道$scope.profile
正在设置中,因为我在html中打印它。
答案 0 :(得分:2)
使用$scope.$on
来观察范围变量,而不是$scope.$watch
。
$scope.$watch("scope_variable_name", function(newVal, oldVal){
// Add your code here
});