This content originally appeared on DEV Community and was authored by Dany Paredes
One of the easiest things is eating a burger, the burger has the same base but changes its content, so let's build our burger component.
Today’s we explain how to use ng content to provide an area flexible and multiple slot.
- Use ng content for content projection.
- Use multiple slot for content projection.
If you want to play with the https://burger-angular-content-projection-demo.stackblitz.io
Our scenario.
We have a list of ingredients components
to be use for prepare our burger component
.
<top-bun></top-bun>
<cheese></cheese>
<bottom-bun></bottom-bun>
<tomato></tomato>
<onion></onion>
<meat></meat>
The main idea is create a burger component and put our ingredients inside of him, like a normal burger using two great angular feature content projection.
Content Projection and ng-content
Angular allows us to make our component reusable using content projection, it allows declaring an area to be suitable for changes, inside or a component.
It helps us provide a wrapper, and we define which information or piece is part of the component.
Using the ng-content tag, we tell to angular to replace the ng-content area with our elements or component.
To begin, first create the burger component and use the tag ng-content into the template.
import { Component } from '@angular/core';
@Component({
selector: 'burger',
templateUrl: './burger.component.html',
})
export class BurgerComponent {}
<div class="burger">
<ng-content></ng-content>
</div>
Perfect, step complete, move the ingredients to body of the burger component.
<burger>
<top-bun></top-bun>
<cheese></cheese>
<tomato></tomato>
<onion></onion>
<meat></meat>
<bottom-bun></bottom-bun>
</burger>
It works, the burger component allow have child elements components into it and the burge acts as wrapper.
Why the meat is not in the middle ?
Multiple slots
All burgers have the meat in the center, we want to be flexible to add components or elements but without break our burger, so the burger component need to render elements but in specific area.
The ng-content have a optional property selector
, get the content related to css selector like class or attributes, so we define the areas.
Update the default burger with the following:
- add top-bun and bottom-bun as default for all burgers.
- 3 ng-contents with the selector top, middle and botton
- ng content for the price
- default ng-content for ingredients without location.
<div class="burger">
<top-bun></top-bun>
<ng-content select="[top]"></ng-content>
<ng-content select="[middle]"></ng-content>
<ng-content select="[bottom]"></ng-content>
<bottom-bun></bottom-bun>
</div>
<div class="price">
<ng-content select="[price]"></ng-content>
</div>
<div>
<p>These ingredients don't have a location</p>
<ng-content></ng-content>
</div>
We have ready our burger component, if some ingredients or piece don't have location it will move to in other div.
The tomato and the onion don't have attribute so, go to the default ng-content.
<!-- Burger with ingredients without location.-->
<burger>
<tomato></tomato>
<cheese top></cheese>
<meat middle></meat>
<onion></onion>
<span price>4€</span>
</burger>
Done!
We have a reusable component with content projection reusable to build new burgers, also force to elements to be located in a specific area and default location.
That's it! Hopefully, give you a bit of help with ng-content and make your components flexible.
If you enjoyed this post, share it!
Photo by Haseeb Jamil on Unsplash
This content originally appeared on DEV Community and was authored by Dany Paredes
Dany Paredes | Sciencx (2021-09-27T15:13:40+00:00) How to use Angular content projection for prepare a burger. Retrieved from https://www.scien.cx/2021/09/27/how-to-use-angular-content-projection-for-prepare-a-burger/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.