在Angular中的兄弟范围之间进行通信 [英] Communicating between sibling scopes in Angular

查看:52
本文介绍了在Angular中的兄弟范围之间进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是如何在作为范围兄弟的两个组件之间正确通信的吗?

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


  1. Ctrl1将事件发送到Ctrl1和Ctrl2。

  2. 父作用域侦听来自Ctrl1的发出事件,然后将另一个事件向下广播到Ctrl2。

  3. Ctrl2侦听来自父作用域的广播事件。

推荐答案

是的,这是我在Angular兄弟范围之间进行通信的方式。通常我认为Ctrl1向其所有后代范围发出'up',并且'on'在监听事件的父范围内,父范围向所有子范围广播'down'。在这种情况下,一旦听到事件,Ctrl2应该在'on'上设置一些东西来做某事。

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.

作为旁注,我做过类似的事情。我已经将rootScope用作集中式事件总线,它可以监听不同的子范围事件,然后再执行一些任务或广播。然后,子范围将负责简单地发出rootScope。

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.

这篇关于在Angular中的兄弟范围之间进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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