我想$scope.me被动地代表当前登录的用户,以便当用户注销并以另一个用户身份重新登录时,此变量会更新。现在,当用户注销并以另一个用户身份重新登录时,旧值$state.me仍然存在。重新加载页面后,此值将被更正。我该如何解决?
这是我在控制器中的工作,糟糕的解决方案:
$scope.$on('$ionicView.enter', function(e) {
if($rootScope.currentUser)
$scope.me = $rootScope.currentUser;
});
这有效,但每次用户转换到此状态时都会重置变量......一个丑陋的非流星解决方案。
这是我目前的尝试:
$scope.me = ($scope.$meteorCollection(function(){
return Meteor.users.find({_id: Meteor.userId()});
}))[0];
这应该可以工作,因为 Meteor.userId() 是反应性的,并且应该强制它的父函数在它发生变化时重新运行,从而$scope.me实时更正......但它没有。
而是$scope.me更新为旧用户的经过清理的配置文件......除了他们的_id和profile可见之外什么都没有。这告诉我$scope.$meteorCollection正在重新运行,但旧值为Meteor.userId().
我错过了什么?谢谢!
* 编辑 * 这是一个转折点
$scope.me = ($scope.$meteorCollection(function(){
console.log("$scope.me reset to "+Meteor.userId());
return Meteor.users.find({_id: Meteor.userId()});
}))[0];
当用户切换时,将新用户的 ID打印到控制台,但即使使用正确的值重新运行查询,仍会返回旧用户。