是否有用于angular的事件发布/订阅框架? [英] Is there an event pub/sub framework for angular?

查看:86
本文介绍了是否有用于angular的事件发布/订阅框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自WPF背景,在Prism中,您具有IEventAggregator接口.您定义可以从控制器预订的事件,然后从另一个控制器发布该事件.这是彼此不认识的控制器之间进行通信的一种方式.

Angular JS中有类似的东西吗?

我有一个index.html页面的顶级控制器,该页面包含三个子面板,每个子面板都有自己的控制器.我希望顶级控制器能够在面板控制器上调用refresh(),但是它没有对那些控制器的引用.

解决方案

我认为您可以使用$ scope.$ broadcast向儿童控制者发送动作;

通过

发送数据之后

  $ scope.$ broadcast('customEvent',{命令:重新加载"}); 

您可以在子控制器中设置监听器

  $ scope.$ on('customEvent',function(event,data){if(data ==='reload'){//重新加载}}); 

好的解释在这里: http://toddmotto.com/all-about-angulars-在发布订阅上发出广播/

I am coming from a WPF background where in Prism you have the IEventAggregator interface. You define events that you can subscribe to from controllers, and then you publish the event from another controller. It is a way to communicate between controllers that do not know about each other.

Is there anything similar in Angular JS?

ie I have a top level controller for an index.html page that contains three sub panels, each with their own controller. I want the top level controller to be able to call refresh() on the panel controllers but it has no reference to those controllers.

解决方案

I think you can use $scope.$broadcast to send action to children contorllers;

After you send data by

$scope.$broadcast('customEvent', {
  command: 'reload'
});

you can set listener in child controller

$scope.$on('customEvent', function (event, data) {
  if(data === 'reload'){
     //reload
  }
});

Good explanation is here: http://toddmotto.com/all-about-angulars-emit-broadcast-on-publish-subscribing/

这篇关于是否有用于angular的事件发布/订阅框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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