【发布时间】:2018-02-14 02:51:46
【问题描述】:
我有一个针对父作用域的指令,我也有另一个针对子作用域的指令。在一个模板中,我有几个父子范围。像这样。
ParentScope1
- ChildScope1
ParentScope2
- ChildScope2
如果我更改 Parent 中的值,我会将其广播给 Child。我正在使用$rootScope.$broadcast 从父母那里广播。我正在使用$rootScope.$on 来接受孩子的这种变化。
我的问题是:
现在,如果我更改 ParentScope1 中的值,它将广播到 ChildScope1。然后我将在ParentScope2中更改一个值,它会广播到ChildScope2,但它也会广播到ChildScope1。
我想要:更改 ParentScope1 中的值,它将广播到 ChildScope1。更改 ParentScope2 中的值,它将广播到 ChildScope2。 我在网上搜索了一段时间,但没有找到解决方案。也许我没有使用正确的关键字来搜索它。请指教。谢谢。
【问题讨论】:
-
为什么使用
$rootScope而不是$scope? Check out this article 获取有关 angularjs 事件系统的更多信息。 -
@StanislavKvitash 我不知道,因为这是一个遗留代码,而编写它的人已经不在了。
-
@StanislavKvitash 我不认为如果我使用 $scope 会解决这个问题?
标签: javascript angularjs angularjs-rootscope