Is there an event pub/sub framework for angular? [closed]

谁都会走 提交于 2019-12-24 16:26:39

问题


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.


回答1:


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/



来源:https://stackoverflow.com/questions/33958805/is-there-an-event-pub-sub-framework-for-angular

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