Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

angular

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. There is an array of ContentChildren that I have which I can loop through.

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?

like image 537
ktamlyn Avatar asked Jan 05 '23 03:01

ktamlyn


1 Answers

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). EventEmitters are observables, so you can simply subscribe to the stream of events.

like image 123
ktamlyn Avatar answered May 15 '23 09:05

ktamlyn