尝试从另一个控制器angularjs访问变量时获取未定义的变量

时间:2015-07-01 21:50:08

标签: angularjs angularfire

您好我是angularjs的新手,任何帮助将不胜感激。我正在使用服务授权用户,我的服务看起来像这样

'use strict';
app.factory('Auth', function ($firebaseSimpleLogin, FIREBASE_URL, $rootScope, $firebase) {
    var ref = new Firebase(FIREBASE_URL);
    var auth = $firebaseSimpleLogin(ref);
    var Auth = {
        register: function (user) {
            return auth.$createUser(user.email, user.password);
        },
        createProfile: function (user) {
            var profile = {
                userfirstname: user.fname,
                userlastname: user.lname,
                username: user.fname + " " + user.lname,
                userprofiletags: user.profiletags
            };
            var profileRef = $firebase(ref.child('profile'));
            return profileRef.$set(user.uid, profile);
        },
        login: function (user) {
            return auth.$login('password', user);
        },
        logout: function () {
            auth.$logout();
        },
        resolveUser: function () {
            return auth.$getCurrentUser();
        },
        signedIn: function () {
            return !!Auth.user.provider;
        },
        user: {}
    };
    $rootScope.$on('$firebaseSimpleLogin:login', function (e, user) {
        console.log('logged in');
        angular.copy(user, Auth.user);
        Auth.user.profile = $firebase(ref.child('profile').child(Auth.user.uid)).$asObject();
        console.log(Auth.user);
    });
    $rootScope.$on('$firebaseSimpleLogin:logout', function () {
        console.log('logged out');
        if (Auth.user && Auth.user.profile) {
            Auth.user.profile.$destroy();
        }
        angular.copy({}, Auth.user);
    });
    return Auth;
});

我正在尝试从控制器访问userprofiletags变量,如下面编码

'use strict';
app.controller('PostsCtrl', function ($scope, $http, Post, $location, Auth) {
    $scope.user = Auth.user;
    $scope.signedIn = Auth.signedIn;
    $scope.logout = Auth.logout;
    $scope.usertags = $scope.user.profile.userprofiletags;
    console.log($scope.usertags);
    $scope.loadTags = function (query) {
        return $http.get('support/tags.json');
    };
    $scope.onChange = function (cbState) {
        $scope.message = "The switch is now: " + cbState;
    };
    $scope.posts = Post.all;
    $scope.post = {
        title: '',
        description: '',
        tags: [],
        Anonymous: 'false'
    };
    var createdtime = moment().format('MMMM Do YY, h:mm:ss a');
    $scope.submitQuestion = function () {
        $scope.post.createdTime = createdtime;
        $scope.post.creator = $scope.user.profile.username;
        $scope.post.creatorUID = $scope.user.uid;
        Post.create($scope.post).then(function (ref) {
            $location.path('/posts/' + ref.name());
        });
    };
    $scope.deletePost = function (post) {
        Post.delete(post);
    };
});

当我尝试使用{{user.profile.userprofiletags}}从视图中访问变量时,它会为我提供值。但是当我尝试将变量分配给我的PostsCtrl中的$ scope.usertags时,我得到了未定义的结果。

如果我不够清楚,我会道歉。请帮我这个错误。非常感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

我认为这里的问题是在打电话后控制台.log很短,所以基本上$scope.user.profile.userprofiletags此时并不存在,实际上它会在广播这个事件时准备就绪'$firebaseSimpleLogin:login'

你可以使用$ watch并在收到数据后将其销毁

watchDestroyer = $scope.$watch('user.profile', function (newUserProfile) {
    if (typeof newUserProfile !== "undefined" && newUserProfile !== null) {
        $scope.user = newUserProfile
        watchDestroyer()
    }
})

或利用该事件或使用承诺

答案 1 :(得分:0)

由于Firebase调用是异步的,因此此时Auth.user未定义。您可以在控制器中执行以下操作:

Auth.resolveUser().then(function(user) {
    $scope.usertags = user.profile.userprofiletags;
});