简体   繁体   English

多个父子范围事件触发问题

[英]Multiple parent-child scope event trigger issue

I have a directive for parent scope, I also have another directive for child scope. 我有一个父范围的指令,也有另一个子范围的指令。 In a template, I have several parent-child scope. 在模板中,我有几个父子范围。 Like this. 像这样。

ParentScope1
 - ChildScope1
ParentScope2
 - ChildScope2

If I change a value in Parent, I will broadcast it to Child. 如果更改“父级”中的值,则将其广播给“子级”。 I am using $rootScope.$broadcast to broadcast from parent. 我正在使用$rootScope.$broadcast broadcast从父级广播。 I am using $rootScope.$on to accept this change in child. 我正在使用$rootScope.$on接受此子项更改。

My problem is: 我的问题是:

Now, If I change a value in ParentScope1, it will broadcast to ChildScope1. 现在,如果我更改ParentScope1中的值,它将广播到ChildScope1。 Then I will change a value in ParentScope2, it will broadcast to ChildScope2, but it will also broadcast to ChildScope1. 然后,我将在ParentScope2中更改一个值,该值将广播到ChildScope2,但也将广播到ChildScope1。

I want: Change a value in ParentScope1, it will broadcast to ChildScope1. 我想要:更改ParentScope1中的值,它将广播到ChildScope1。 Change a value in ParentScope2, it will broadcast to ChildScope2. 更改ParentScope2中的值,它将广播到ChildScope2。 I search online for some time but did not find the solution for it. 我在网上搜索了一段时间,但没有找到解决方案。 Maybe I did not use the correct keywords for searching it. 也许我没有使用正确的关键字进行搜索。 Please advise. 请指教。 Thank you. 谢谢。

In your definition of directive set 在您的指令集定义中

scope : true

then use 然后使用

$scope.$broadcast
$scope.$on

this should probably works fine 这可能应该很好

post your code so we have a better view of the problem 发布您的代码,以便我们更好地了解问题

You are looking for communication between parent and child directive, you can use below approach but both directive will be tightly coupled using this- 您正在寻找父子指令之间的通信,可以使用以下方法,但是两个指令将通过以下方式紧密结合:

Require a controller - Get the handle of same node of parent node directive controller. 需要控制器 -获取父节点指令控制器的同一节点的句柄。 Require : '^parnetDireName' is used to find the controller on parent node. 要求 :'^ parnetDireName'用于在父节点上查找控制器。 Without ^, it will find for same node only. 如果不使用^,它将仅查找相同的节点。 '?' '?' is used when controller may not be available.''?^", "?^^", "^^". Fourth parameter of link function is used to get the controller. It can use the controller prop/method. You can also access multiple controller because Require will have array - require: ['^dir1','^dir2']. Link function will have cntrl array and it can be access through array element in the same sequence ?^“,”?^^“,” ^^“。链接函数的第四个参数用于获取控制器。它可以使用控制器的prop /方法。访问多个控制器,因为Require将具有数组-require:['^ dir1','^ dir2']。 链接函数将具有cntrl数组,并且可以按相同顺序通过数组元素进行访问

Pre link and post link function for nested directive - 嵌套指令的前链接和后链接功能 -

  • Default link function is post link function. 默认链接功能是发布链接功能。
  • Use keyword post to define it explicitly 使用关键字发布明确定义它
  • Child post link function is executed first if parent and child both has Post link function 如果父母和孩子都具有邮政链接功能,则首先执行子邮政链接功能
  • Parent link function is executed first if both parent and child has pre-link function. 如果父子都具有预链接功能,则首先执行父链接功能。
  • Controller is executed before link function 在链接功能之前执行控制器

--------------------------------- Another decoupled way --------------------- There are three ways to setup the relation between directive scope and containing controller scope- - Directive with shared scope with containing controller. --------------------------------- 另一种解耦方式 -------------- -------可以通过三种方式来设置指令范围和包含控制器范围之间的关系--与包含控制器共享范围的指令。 Any new 任何新
item/modified item by directive will be part of parent scope. item /通过指令修改的item将成为父范围的一部分。 It is default or scope:false - Directive with inherited scope . 它是default或scope: false-具有继承的scope的指令。 Any new item added by directive will not be visible by containing controller. 指令添加的任何新项目都将无法通过包含控制器看到。 Directive scope can read all data from parent scope. 指令范围可以从父范围读取所有数据。 Use scope:true property to activate it. 使用scope:true属性将其激活。 Child can see parent data and can override or create new variable. 子级可以看到父级数据,并且可以覆盖或创建新变量。
- Isolated scope . - 隔离范围 Both scope cannot read each other data. 两者都无法互相读取数据。 Object mapping is required to read the parent data. 需要对象映射才能读取父数据。 Directive scope mapping will have object name and same object will be passed from html. 指令范围映射将具有对象名称,并且将从html传递相同的对象。 There are three ways to receive the parameter- - Complete object Data -> '=' is used - Simple value like flag as a String- '@' is used . 有三种接收参数的方法--使用完整对象数据 ->'='-使用简单值(例如标记为字符串)-'@'。 '@sampleVar', where sampleVar is the name of variable in the html. '@sampleVar',其中sampleVar是html中变量的名称。 Scope { cntrollerStrVarName: '@htmlStrVarName' } 范围{cntrollerStrVarName:'@htmlStrVarName'}
- Function parameter - '&' is used to pass the parameter. - 函数参数 -'&'用于传递参数。 Method parameter can be overridden using ({paramName: 'value'}) 可以使用({paramName:'value'})覆盖方法参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM