A T
A T

Reputation: 13826

Content of component in Angular? <foo>content</foo>

How do I get the contents of my current component?

Say I have FooComponent that's used in another component, like so:

<foo>bar</foo>

How do I extract bar in FooComponent?

Tried this, but got val = null:

ngAfterViewInit() {
    console.info('val =', this.elementRef.nativeElement.value);
}

Upvotes: 0

Views: 278

Answers (2)

arctic_Oak
arctic_Oak

Reputation: 1024

I've created one stackblitz for you in which I've created a tabs component and that tabs component has 3 tabs and I'm passing 3 different templates from app component to tabs component. I think this will give you a better understanding of passing data to a component and how to create a custom component.

https://stackblitz.com/edit/angular-ck6nch?file=app%2Fapp.component.ts

Upvotes: 0

A T
A T

Reputation: 13826

Ahh, found it:

this.elementRef.nativeElement.innerHTML

And to the template add:

<ng-content></ng-content>

Upvotes: 1

Related Questions