类似于这样的情况
<p ng-controller="parentCtr">
<p ng-controller="childCtr1">
<p ng-controller="childCtr2">
</p>
</p>
</p>
我需要的是把parentCtr里的变量传给childCtr2
既然childCtr2可以继承parentCtr的scope,那么什么情况下用broadcast
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
前提:
broadcast是将 事件广播给子scope。childCtr2 可以 通过继承 反问到 parentCtr 的变量,但但parentCtr中变量发生变化,或者parentCtr中触发事件需要给childCtr2的时候,childCtr2是不知道的(PS: 当然你可以
setTimeout一直查询parentCtr变量是否有变化)。broadcast就是这个时候使用,但parentCtr 有变化(比如监听了事件)需要告知childCtr2时,就可以使用broadcast告知childCtr2,当然childCtr2 要对应监听parentCtrbroadcast过来的内容。broadcast 是事件广播,比如你不同的模块之间想通信,可以用这个。