我想在登录后让所有视图都可以使用用户信息。如何修改代码以便能够从其他视图访问pseudonym
?
你能举个例子吗?
这是我的登录控制器:
app.controller("MyregisterCtrl", ["$scope", "$stateParams", "Auth", "$state", "$location", "$modal", "DatabaseRef",
function ($scope, $stateParams, Auth, $state, $location, $modal, DatabaseRef) {
$scope.user = {};
$scope.signIn = function () {
if (!$scope.user.email && !$scope.user.password) {
toastr.error("Add email and password");
} else {
Auth.$signInWithEmailAndPassword($scope.user.email, $scope.user.password)
.then(function(firebaseUser) {
//=====user info=================
var userId = firebase.auth().currentUser.uid;
DatabaseRef.ref('/users/' + userId).once('value')
.then(function(snapshot) {
pseudonym = snapshot.val().pseudonym;
console.log("pseudonym: ", pseudonym);
return pseudonym;
});
//======================
$state.go('app.dashboard');
if (!firebaseUser.emailVerified) {
toastr.info('Your email is NOT verified.', 'Verify email!');
$state.go('login.signin');
}
})
.catch(function(error) {
toastr.error(error.message, error.reason, { timeOut: 10000 });
$scope.user = {};
})
}
};
}]);
此console.log("pseudonym: ", pseudonym);
为我提供了我想要访问的内容,但无法通过输入{{falseonym}}来访问其他视图。
答案 0 :(得分:1)
只要您想在视图上显示,就可以分配给$ scope变量
pseudonym = snapshot.val().pseudonym;
$scope.pseudonym =pseudonym;