is it possible to use getReactively on $rootScope

2019-06-08 22:27发布

I have a case that I want to update the ui based on the user so if a user was log-out and log-in as different user, a score of the user in the ui is changed. The way I thought about doing it is by looking at $rootScope.currentUser.

from my testing it seems like there is some kind of access to the $rootScope, but it is not getting updated on user change (unless I have a bug in my code). here is the relevant code:

$meteor.autorun($scope, function() {
            if (null !== $rootScope.currentUser) {
                $scope.userForScore = $scope.$meteorObject(Userinfo, {user_id: $scope.getReactively('currentUser')._id});//$rootScope.currentUser._id
            }
        });

    $scope.userScore = function(){
        if ($scope.userForScore === undefined || $scope.userForScore.score === undefined) {
            return 1;
        }
        return $scope.userForScore.score;
    };

Userinfo - is a collection I created in the server.

2条回答
劫难
2楼-- · 2019-06-08 23:20

You can use Meteor.user() which is a reactive var. No need to add an additional layer of watchers.

查看更多
我命由我不由天
3楼-- · 2019-06-08 23:32

Yes it is possible and it works exactly like you did it.

Your problem was unrelated to getReactively, I've created a pull request to your repo to fix it: https://github.com/Urigo/angular-meteor/issues/415#issuecomment-113925910

查看更多
登录 后发表回答