Communicating between sibling scopes in Angular

前端 未结 2 1296
野趣味
野趣味 2021-01-01 21:45

Is this how to correctly communicate between two components that are scope siblings?

  1. Ctrl1 emits event up to parent scope of both Ctrl1 and Ctrl2.
  2. Par
2条回答
  •  情深已故
    2021-01-01 22:41

    Yes, this is how I communicate between sibling scopes in Angular. Typically I think of Ctrl1 as emitting 'up' to all its descendant scopes and 'on' a parent scope listening to the event, the parent scope broadcasting 'down' to all children scopes. In this case, Ctrl2 should have something set up on 'on' to do something once it hears the event.

    As a side note, I've done something similar where I've used the rootScope as a centralized event bus where it listens to different children scope's events and then performs some task or broadcasts down again. The children scopes would then be responsible for simply emitting up to the rootScope.

提交回复
热议问题