简体   繁体   English

AngularJS:在组件之间进行通信的最佳实践是什么?

[英]AngularJS: What is the best practice to make communication between components?

I have a problem reasoning about components communication. 我有一个关于组件通信的问题。 The main question I tried to reason about and failed: what should I use - $watch or $on ($broadcast/$emit) to establish the communication between components? 我试图推理和失败的主要问题是: 我应该使用什么 - $ watch$ on ($ broadcast / $ emit)来建立组件之间的通信?

I see three basic cases: 我看到三个基本情况:

  1. Controller + Directive . 控制器+指令 They communicate naturally using the $scope bidibinding. 它们使用$ scope bidibinding自然地进行通信。 I just put the service, which incapsulates some shared state, in the $scope using some object ( $scope.filter = {} ). 我只是使用一些对象( $scope.filter = {} )将服务(包含一些共享状态)放在$ scope中。 This approach seems very reasonable to me. 这种方法对我来说似乎很合理。

  2. Controller + Controller . 控制器+控制器 I use the DI to inject singleton services with incapsulated state to communicate between controllers. 我使用DI来注入具有封装状态的单件服务,以便在控制器之间进行通信。 Those services are bounded to directives using the previous approach. 这些服务受限于使用先前方法的指令。 This gives me the data binding out-of-the-box. 这为我提供了开箱即用的数据绑定。

  3. Directive + Directive . 指令+指令 This is the blind spot in my reasoning. 这是我推理的盲点。 I have directives, that reside in different scopes, in the same scope, etc. I have directives that must reflect all changes (think about slider + charts) and directives, that must trigger the http request (think about select input). 我有指令,它们位于不同的范围内,在相同的范围内等。我的指令必须反映所有必须触发http请求的变化(想想滑块+图表)和指令(想想选择输入)。

So, the questions are: 所以,问题是:

  1. What should I use - $watch or $on ($broadcast/$emit) to establish the communication between components? 我应该使用什么 - $ watch$ on ($ broadcast / $ emit)来建立组件之间的通信?
  2. Should I tend to use $watch in directive-to-directive communication? 我应该倾向于在指令到指令的沟通中使用$ watch吗?
  3. Or should I tend to use $broadcast in directive-to-directive case? 或者我是否倾向于在指令到指令的情况下使用$ broadcast
  4. Is it better to share the state using injection+binding or injection+events ? 使用注入+绑定注入+事件共享状态更好吗?

I think this depends on the use case for your directives/components. 我认为这取决于您的指令/组件的用例。 If you expect to be able to re-use a component without having to modify the scope that the component lives in then using broadcast/emit/on would make more sense. 如果您希望能够重新使用组件而无需修改组件所在的范围,那么使用broadcast / emit / on会更有意义。 IMO if a component internally has some information that I want to be able to retrieve and do different things with, then the broadcast/emit/on scheme makes the most sense. IMO如果组件内部有一些我希望能够检索并执行不同操作的信息,则广播/发射/开启方案最有意义。

If on the other hand I need to trigger some service calls in response to something in a directive or I want to share state between a couple of views I end up using a service. 另一方面,如果我需要触发一些服务调用以响应指令中的某些内容,或者我想在几个视图之间共享状态,我最终使用服务。

As noted in the comments another alternative that exists is using the require property in the directive definition object: 如注释中所述,另一种替代方法是在指令定义对象中使用require属性:

require: 'siblingDirectiveName', // or // ['^parentDirectiveName', '?optionalDirectiveName', '?^optionalParent']

require - Require another directive and inject its controller as the fourth argument to the linking function. require - 需要另一个指令并将其控制器作为链接函数的第四个参数注入。 The require takes a string name (or array of strings) of the directive(s) to pass in. If an array is used, the injected argument will be an array in corresponding order. require需要传入指令的字符串名称(或字符串数​​组)。如果使用数组,注入的参数将是相应顺序的数组。 If no such directive can be found or if the directive does not have a controller, then an error is raised. 如果找不到这样的指令或指令没有控制器,则会引发错误。 The name can be prefixed with: 该名称可以带有以下前缀:

  • (no prefix) - Locate the required controller on the current element. (无前缀) - 在当前元素上找到所需的控制器。
  • ? - Attempt to locate the required controller, or return null if not found. - 尝试找到所需的控制器,如果未找到则返回null。
  • ^ - Locate the required controller by searching the element's parents. ^ - 通过搜索元素的父项找到所需的控制器。
  • ?^ - Attempt to locate the required controller by searching the element's parents, or return null if not found. ?^ - 尝试通过搜索元素的父项来查找所需的控制器,如果未找到则返回null。

This can be useful in cases where you're creating a "compound component" where multiple directives make more sense than trying to encapsulate all of the functionality into one directive, but you still require some communication between the "main/wrapping directive" and it's siblings/children. 这在您创建“复合组件”的情况下非常有用,其中多个指令比尝试将所有功能封装到一个指令中更有意义,但您仍然需要在“main / wrapping指令”之间进行一些通信。兄弟姐妹/孩子。

I know this isn't a clear cut answer but I'm not sure that there is one. 我知道这不是一个明确的答案,但我不确定是否有一个。 Open to edits/comments to modify if I'm missing some points. 如果我遗漏了一些要点,可以修改编辑/评论。

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

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