AngularJS $广播和$上没有更新$范围 [英] AngularJS $Broadcast and $on not updating $scope

查看:131
本文介绍了AngularJS $广播和$上没有更新$范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更新与该用户的姓名主导航栏,同时还更新他多少项目购买了购物车图标。使用$广播和$之前我还没有尝试过。

I'm attempting to update the main nav bar with the user's name, and also update the shopping cart icon with how many items he's bought. I haven't tried using $broadcast and $on before.

在我中设置了一个简单的广播日志成功后,这是它自己的登录控制器:

On successful post of a log in I set up a simple broadcast, this is in its own login controller:

$rootScope.$broadcast('user-logged-in');

现在我有在购物车图标所在的控制器事件$,但值永远不会在日志中更新。事实上,我甚至不获得控制台消息,所以它只是从来没有得到它。

Now I have the $on event in the controller where the shopping cart icon resides, but the value is never updated upon log in. In fact, I don't even get the console message, so it just never gets to it.

$scope.$on('user-logged-in', function  (event, args) {
            console.log("Broadcasted to Products Controller");
            $scope.logged= Session.isLogged();
            $scope.username= Session.recallName();
        });

学习如何使用,这将是非常有益的,因为有其它更多我一直更新主视图时,在其他视图中的数据发生改变。如果让我用UI的路由器的差异。

Learning how to use this would be very helpful, because there is lots more I keep to update on the main view when data in other views change. If it makes a difference I'm using UI-Router.

推荐答案

您正在为广播 $ rootScope 所以 $ rootScope。$上( ...

这篇关于AngularJS $广播和$上没有更新$范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆