34
loading...
This website collects cookies to deliver better user experience
ng-content
is used to create configurable components. If we use the ng-content
tag in our template, the inner content of the tags that define our component are then projected into this space.//app-modal.html
<div>
<div class="header"> Amazing Header </div>
<div class="body">
<p> Hi Friends! How are you today </p>
</div>
<div class="footer">This is our footer</div>
<div>
ng-content
comes to our aid.//app-modal.html
<div>
<div class="header"> Amazing Header </div>
<div class="body">
<ng-content> </ng-content>
</div>
<div class="footer">This is our footer</div>
<div>
<ng-content> </ng-content>
tag. app-modal
component with a custom body, the components will project the content inside the app-modal
and the content projected will be positioned in the place where we have specified the ng-content
tag in the app-modal
as seen below.//app-parent.html
<app-modal>
<div>
<h3> My body intro. </h3>
<p> The perfect paragraph </p>
</div>
</app-modal>
Content projection
. With this, we can build reusable components for a single project or a reusable component library for all our projects. //app-modal.html
<div>
<ng-content> </ng-content>
<div class="body">
<ng-content> </ng-content>
</div>
<ng-content> </ng-content>
<div>
ng-content
provides us with the select
attribute that allows us to specify an identifier for a specific ng-content
tag. Below are various ways in which we can achieve this with different selectors.//app-modal.html
<div>
<ng-content select=".header"> </ng-content>
<div class="body">
<ng-content select="#body"> </ng-content>
</div>
<ng-content select="span"> </ng-content>
<div>
//app-parent.html
<app-modal>
<div class="header"> Our header </div>
<div id="body">
<h3> My body intro. </h3>
<p> The perfect paragraph </p>
</div>
<span> The footer</span>
</app-modal>
ng-content
tag without an identifier (select), it will automatically serves as a default projector for any content that has no identifier.//app-modal.html
<div>
<ng-content select="[slot=head]"></ng-content>
<ng-content select="[slot=body]"></ng-content>
<ng-content select="[slot=footer]"></ng-content>
</div>
//app-parent.html
<app-modal>
<div slot="head"> Our header </div>
<div slot="body"> Our body </div>
<div slot="footer"> Our footer </div>
</app-modal>
//app-modal.html
<div>
<ng-content select="[head]"></ng-content>
<ng-content select="[body]"></ng-content>
<ng-content select="[footer]"></ng-content>
</div>
//app-parent.html
<app-modal>
<div head> Our header </div>
<div body> Our body </div>
<div footer> Our footer </div>
</app-modal>
//app-modal.html
<div>
<ng-content select="app-head"></ng-content>
<ng-content select="app-body"></ng-content>
<ng-content select="app-footer"></ng-content>
</div>
//app-parent.html
<app-modal>
<app-head> Our header </app-head>
<app-body> Our body </app-body>
<app-footer> Our footer </app-footer>
</app-modal>
ng-content
You can build your own reusable components without you worrying about the contents or the styles of the contents the users needs to project.