在触发事件中,可以选择$broadcast/$emit/$on

时间:2022-01-25 17:36:45

What is an alternative way in AngularJS to trigger events other than $broadcast/$emit/$on. If I will have too many event triggers between controllers and/or directives, I'm afraid this will lead to hard to trace spaghetti events. (Or could it be that too many events is a bad design after all?)

在AngularJS中,除了$broadcast/$emit/$on之外,还有什么其他的触发方式呢?如果我在控制器和/或指令之间有太多的事件触发器,我担心这将导致难以追踪意大利面条事件。(或者,过多的活动毕竟是一个糟糕的设计?)

I heard using service/factory is the preferred way. Can someone give me an example how? Or are there other methods aside from service/factory?

我听说用服务/工厂是最好的方式。有人能给我举个例子吗?或者除了服务/工厂还有其他方法吗?

1 个解决方案

#1


3  

You could use a event queue/event bus as a solution. Check out

您可以使用事件队列/事件总线作为解决方案。看看

http://jonathancreamer.com/an-angular-event-bus-with-postal-js/ and https://github.com/jseppi/angular-queue

http://jonathancreamer.com/an-angular-event-bus-with-postal-js/和https://github.com/jseppi/angular-queue

#1


3  

You could use a event queue/event bus as a solution. Check out

您可以使用事件队列/事件总线作为解决方案。看看

http://jonathancreamer.com/an-angular-event-bus-with-postal-js/ and https://github.com/jseppi/angular-queue

http://jonathancreamer.com/an-angular-event-bus-with-postal-js/和https://github.com/jseppi/angular-queue