简体   繁体   English

如何在不通过模板连接的情况下监听ContentChild事件?

[英]How do I listen to a ContentChild event without hooking up via template?

I need to listen for an event from an array of tab components. 我需要侦听来自选项卡组件的事件。 These tabs emit "onMinimized" however I would rather hook up to these events in one line of code than entering (onMinimized)="hide" for each tab component entry in the template. 这些选项卡发出“ onMinimized”,但是我宁愿在一行代码中挂接这些事件,而不是为模板中的每个选项卡组件条目输入(onMinimized)="hide" There is an array of ContentChildren that I have which I can loop through. 我有一组ContentChildren,可以循环遍历。

I want to do something similar to has follows in the parent: 我想做一些类似的事情在父母:

@ContentChildren(TabComponent) tabs: QueryList<TabComponent>;

...

ngAfterContentInit() {
  this.tabs.map((t: TabComponent) => {
    t.addEventListener("onMinimized", this.hide);
  });
}

hide() {
  // hide tabs
}

Thoughts? 有什么想法吗?

Figured it out after some trial and error. 经过一番尝试和错误后弄清楚了。

  ngAfterContentInit() {
    this.tabs.map((t: TabComponent) => {
      t.minimizeTab.subscribe(() => { this.hide(); })
    });
  }

The way to approach the problem was to consider that the component had a property which was an EventEmitter (just like all angular Outputs). 解决该问题的方法是考虑组件具有EventEmitter的属性(就像所有角度输出一样)。 EventEmitters are observables, so you can simply subscribe to the stream of events. EventEmitter是可观察的,因此您可以简单地订阅事件流。

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

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