Getting a projected header element using @ContentChild?

55 Views Asked by At

Within this component demo I'm trying to grab the projected header element like this:

@ContentChild('header') header!: HTMLElement;

The demo tries to log the element within the ngAfterContentInit() method like this:

  ngAfterContentInit() {
    console.log(this.header);
  }

Not getting any love, even though the header projects fine within within the main.ts demo component.

  <my-test>
  <header>header</header>
  Test
  </my-test>

Any ideas?

1

There are 1 best solutions below

5
Matthieu Riegler On BEST ANSWER

The selector of a query needs to be a class of a template variable.

In your case to fix the issue, you'll need to add a template variable #header to your header.

<header #header>header</header>