Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert html in a child component in angular?

I'm trying to insert html into a child component. Basically something like this:

<custom-container><p>Parnet content</p></custom-container>

I can find references for passing data to child component but not able to find how to pass actual html inside my selector?

like image 579
Adam Boostani Avatar asked Jul 09 '17 05:07

Adam Boostani


People also ask

How do you pass data to a child component?

To let Angular know that a property in a child component or directive can receive its value from its parent component we must use the @Input() decorator in the said child. The @Input() decorator allows data to be input into the child component from a parent component.


2 Answers

You can also use <ng-content> with select attribute

Parent Component:

<child-component>
    <div sample>Content form parent</div>
</child-component>

Child Component:

something..
<ng-content select="[sample]"></ng-content>
some more...

Output:

something..
<div>Content from parent</div>
some more...

More details here

like image 170
Malai Avatar answered Oct 12 '22 10:10

Malai


ng-content tag can be used in the template of the child component. That tag will take in anything that you put in between your component selector.

like image 32
Adam Boostani Avatar answered Oct 12 '22 12:10

Adam Boostani