is it possible to use getReactively on $rootScope variables?

余生长醉 提交于 2019-12-10 11:55:59

问题


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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/30693154/is-it-possible-to-use-getreactively-on-rootscope-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!