Angularjs 服务变量未在控制器中更新
我有一个NavbarCtrl,它在ng视图之外。我有一个登录控制器,它与服务对话以让用户登录。用户登录后,我希望导航栏使用用户的电子邮件地址进行更新。然而,在我的一生中,我似乎无法在用户登录后使用加载到“Auth”服务中的数据更新Navbar范围 这是我的主index.html:Angularjs 服务变量未在控制器中更新,angularjs,Angularjs,我有一个NavbarCtrl,它在ng视图之外。我有一个登录控制器,它与服务对话以让用户登录。用户登录后,我希望导航栏使用用户的电子邮件地址进行更新。然而,在我的一生中,我似乎无法在用户登录后使用加载到“Auth”服务中的数据更新Navbar范围 这是我的主index.html: <div class="navbar navbar-inverse navbar-fixed-top"> <div class="navbar-inner">
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="#">Brim</a>
<div class="pull-right" ng-controller="NavbarCtrl">
<div ng-click="refresh()">hello</div>
{{ user.email }}
</div>
</div>
</div>
</div>
<div class="container" ng-view>
以及我的登录和导航栏控制器:
function LoginCtrl($scope, $location, Auth) {
$scope.login = function() {
Auth.login($scope.user, function(success) {
if(success) $location.path('/dashboard');
//console.log(Auth.getUser())
});
}
}
function NavbarCtrl($scope, Auth) {
//thought this should work
$scope.user = Auth.getUser();
//experimenting unsuccessfully with $watch
$scope.$watch(Auth.isAuthenticated(),function () {
$scope.user = Auth.getUser()
})
//clicking on a refresh button is the only way I can get this to work
$scope.refresh = function() {
$scope.user = Auth.getUser()
}
}
根据我的研究,我会认为$scope.user=Auth.getUser();可以,但不行,我完全不知道如何在用户登录时更新导航栏。提前感谢您的帮助。尝试更改
$scope.$watch(Auth.isAuthenticated(),函数(){
$scope.user=Auth.getUser()
})
到
$scope.$watch(Auth.isAuthenticated(),函数(){
$scope.user=Auth.getUser()
},对)
查看AungularJs以获得详细解释,默认值为false,这意味着Angular通过引用检测第一个参数值的变化,而传递true则意味着检查是通过相等来完成的。作为函数的参数,检查返回的总是false,因为对函数的引用总是相同的。更新:嗯,你每天都会学到一些新东西。。。只需删除
()
即可查看函数的结果:
$scope.$watch(Auth.isAuthenticated, function() { ... });
$scope.$watch( function() { return Auth.isAuthenticated() }, function() { ... });
在这把小提琴中,请注意当$scope.isAuthenticated的值更改时,“watch1”和“watch3”如何再次触发
因此,这是监视服务上定义的原始值的更改的一般技术:
- 定义返回原语(值)的API/方法
- 注意这个方法
要查看服务上定义的对象或数组的更改,请执行以下操作:
- 定义返回(引用)对象/数组的API/方法
- 在服务中,注意只修改对象/数组,不要重新分配它。
例如,不要这样做:user=
相反,可以这样做:angular.copy(newInfo,user)
或这样做:user.email=…
- 通常,您将为该方法的结果分配一个本地$scope属性,因此$scope属性将是对实际对象/数组的引用
- $watch范围属性
例如:
$scope.user = Auth.getUser();
// Because user is a reference to an object, if you change the object
// in the service (i.e., you do not reassign it), $scope.user will
// likewise change.
$scope.$watch('user', function(newValue) { ... }, true);
// Above, the 3rd parameter to $watch is set to 'true' to compare
// equality rather than reference. If you are using Angular 1.2+
// use $watchCollection() instead:
$scope.$watchCollection('user', function(newValue) { ... });
原始答复:
要查看函数的结果,需要向$watch传递一个封装该函数的函数:
$scope.$watch(Auth.isAuthenticated, function() { ... });
$scope.$watch( function() { return Auth.isAuthenticated() }, function() { ... });
。在小提琴中,请注意当$scope.isAuthenticated的值更改时,只有“watch3”会再次触发。(它们最初都会触发,作为$watch初始化的一部分。)使用user
作为变量名通常不是一个好主意。有一次我更改了它,ticurrentUser
一切都对我有用。我认为这行不通:--在这两种情况下,都返回了对函数的引用,这不会改变。在小提琴中,手表只触发一次(初始化自己)。是的,它工作得很好。谢谢你的解释,还有小提琴,这很有道理。谢谢你的帮助!谢谢,你的解决方案让我今天过得很愉快:简单明了的解释+1值得一提的是,当您需要关注大型集合的更改时,您的更新解决方案可能不是最优的。在这种情况下,使用你原来的方法可能会更好。我不确定我是否得到这个答案。我遇到了一个类似的问题,我只有一个简单的导航栏项: -它不会像原始海报那样刷新。我可以避免调用函数吗?我该怎么称呼呢?有点离题,但刷新时您是否也会失去对已验证用户的跟踪?您最终是否扩展了此功能以利用会话存储?是的,我一直在尝试使用会话存储来跟踪经过身份验证的用户。我遇到过