Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Custom Template Html in Component Selector

Tags:

angular

Is there a way to add custom template html inside a component-selector?

I have a component AppToolbarComponent with selector <app-toolbar> which has a blank template. I would like to use this component on two different components, but with template defined while using the component. For example:

On page-one.component.html:

<app-toolbar>
    <button (click)="onClickOne()">Button in Component One</button>
</app-toolbar>

... and on page-two.component.html:

<app-toolbar>
    <button (click)="onClickTwo()">Button in Component Two</button>
</app-toolbar>

Any ideas?

like image 716
Faisal Avatar asked Dec 31 '25 19:12

Faisal


1 Answers

Based on the comment by @pixelbits, here is an answer:

In <app-toolbar>, add <ng-content> in the component template. Here is an example:

<div>Some Content</div>
<ng-content></ng-content>

Now, anything added inside the <app-toolbar> selector will be rendered inside the <ng-content>. For example:

<app-toolbar>
    <!-- The following content will render inside <ng-content> 
         in the app-toolbar component -->
    <button (click)="onClickOne()">Button in Component One</button>
</app-toolbar>

Here is a link to StackBlitz Demo.

like image 164
Faisal Avatar answered Jan 02 '26 10:01

Faisal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!