global communication in angular module: event bus or mediator pattern/service

前端 未结 3 1482
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-04 22:51

So far I have seen many solutions of the problem. The simplest one is, of course, to $emit an event in $rootScope as an event bus e.g. ( https://gi

3条回答
  •  暖寄归人
    2020-12-04 23:31

    I like an event bus.

    Angular does provide $emit on $rootScope but I don't think that should bound your decision to use it for event-based flows if they are complex or foreseeably complex. Angular has lots of features and while most are great, even the authors admit they're mostly meant to compliment good software engineering principles, not replace them.

    I like this post on using postal.js: An angular.js event bus with postal.js. The two main benefits are channels and envelopes, which will make for more explicit, understandable and flexible event-based logic.

    I find service based approaches to be error prone if state is not managed tightly, which is hard with async calls and injections, where you can't be certain how a service will be multi-purposed in the future.

提交回复
热议问题