简体   繁体   English

获取子元素或组件的焦点信息

[英]Getting focus info of child element or component

I am having a component which may have several child components at same level like我有一个组件,它可能有多个相同级别的子组件,例如

<parent>
  <input />
  <child>
    <input />
  </child>
  <child></child>
  <child></child>
</parent>

Either of child or parent may have input elements. child 或 parent 都可以有input元素。

I am trying to get is if any of input element gets focus then its parent should get a call (other than manual).我试图得到的是,如果任何输入元素获得焦点,那么它的父元素应该得到一个调用(手动除外)。 And if child component gets this info of input getting focus should be able to propagate it to parent without using EventEmitter .如果子组件获得了这个输入获取焦点的信息,应该能够在不使用EventEmitter情况下将其传播给父EventEmitter

I can use event emitter but if nesting of child increases then it would not be a good way.我可以使用事件发射器,但如果孩子的嵌套增加,那么这不是一个好方法。 Can we do it more simpler?我们可以做得更简单吗?

Use:用:

@ViewChild in order to get reference to the Childs and access their data from the Parent. @ViewChild以获取对 Childs 的引用并从 Parent 访问他们的数据。

@Input to pass data from the Parent to the Childs. @Input将数据从父级传递给子级。

@Output in order to trigger events from the Childs to the Parent. @Output以触​​发从 Childs 到 Parent 的事件。

If you don't want to use @Output a workaround that you could use is RxJS BehaviorSubject ( example here ).如果您不想使用 @Output,则可以使用的解决方法是 RxJS BehaviorSubject此处的示例)。 Basically you emit values from the child using that BehaviorSubject and then you subscribe to those changes from the Parent.基本上,您使用该 BehaviorSubject 从子级发出值,然后从父级订阅这些更改。

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

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