Content projection is a technique where you project content that you want to use in another component. For example, you may have a product card component that accepts the contents of another component such as product name, product description, etc.
The various angular content projection types and their implementations are listed below:
Single-slot content projection is the simplest type of content projection. Making a component into which you can project just single content is referred to as single-slot content projection.
🔸 The steps for developing single-slot content projection are as follows:
Step 1 – Create a component
Step 2 – Add an <ng-content> element where you want the projected content to appear
import { Component } from '@angular/core'; @Component({ selector: 'app-single-slot-projection', template: ` <h2>Single-slot content projection</h2> <ng-content></ng-content> ` }) export class SingleSlotProjectionComponent{}
Step 3 – Call <app-single-slot-projection> from <app.component.html> and pass the h1 tag with any message, Wherever you have included “ng-content” in “app-single-slot-projection,” the message will be displayed.
<app-single-slot-projection> <h1>Single slot projection working</h1> </app-single-slot-projection>
An actual DOM element is not created by the placeholder element “ng-content”. Applied custom attributes to “ng-content” are disregarded.
Related read: React Virtual DOM vs Real DOM
What if you want to use your component for content projection, but you have the requirement to project the content in different places, for this, you can use multi-slot content projection.
You can utilize multi-slot content projection if you want to use your component for content projection but you need to project the material in multiple locations.
Several slots can be found in a component. A CSS selector can be specified for each slot to control what content is placed there. Multi-slot content projection is the name of this pattern. You need to specify where you want the projected material to appear when using this pattern. The choose attribute of ng-content> is used to complete this task.
The term “multi-slot content projection” is used to describe this pattern. You must indicate the location of the projected content while using this pattern.
🔸 The steps for developing multi-slot content projection are as follows:
Step 1 – Create a component for multi-slot projection by using the command ng g c multi-slot-projection.
Step 2 – The bootstrap library is what we’re using in this example, but you can use whatever UI framework you choose,
For installing bootstrap use the command npm i @ng-bootstrap/ng-bootstrap.
Step 3 – Open the multi-slot-projection-component.html file and paste the following code.
<div class="alert alert-primary" role="alert"> <ng-content select="[description]"></ng-content> </div> <ul class="list-group shadow"> <li class="list-group-item active" aria-current="true"> <ng-content select="[title]"></ng-content> </li> <li class="list-group-item">A second item</li> <li class="list-group-item">A third item</li> <li class="list-group-item">A fourth item</li> <li class="list-group-item">And a fifth one</li> </ul>
Element that uses the description attribute is projected into the <ng-content> element with the select=[description] attribute, while content that uses the title attribute is projected into the <ng-content> element with the select=[title] attribute.
Step 4 – Open app.component.html and paste the following code.
<!-- As a heading --> <nav class="navbar navbar-dark bg-primary"> <div class="container-fluid"> <span class="navbar-brand mb-0 h1">Multi Slot Content Projection</span> </div> </nav> <div class="container"> <div class="row mt-5"> <div class="col-md-6"> <app-multi-slot-projection> <span title>Manager's</span> <span description>List of all working managers</span> </app-multi-slot-projection> </div> <div class="col-md-6"> <app-multi-slot-projection> <span title>Employee's</span> <span description>List of all working employees</span> </app-multi-slot-projection> </div> </div> </div>
You can see that we called “app-multi-slot-projection” twice and passed different values for the attributes “description” and “title”.
We passed different values for the descriptions “list of all working managers” and “list of all working employees,” respectively, for the title “Managers” and “Employees”.
Content projection in angular provides a powerful mechanism for reusability and flexibility in component-based development.
In this article, we explored two types of content projection: single-slot and multi-slot.
With single-slot content projection, we learned how to create a component that accepts and displays a single projected content.
Moving on to multi-slot content projection, we discovered a more advanced technique that allows us to project content into multiple slots within a component.
Content projection in Angular is a powerful feature that allows you to create more flexible and reusable components by allowing external content to be inserted into a component’s template.
Content projection in Angular enhances component reusability, allows dynamic template creation, separates concerns, promotes consistent UI patterns, and facilitates parent-child communication, leading to more flexible, maintainable, and adaptable components.
Content projection in Angular offers several benefits that enhance component reusability, flexibility, and maintainability: Component Reusability, Separation of Concerns, Dynamic Templates, Consistent UI Patterns, Ease of Testing etc.
How to Effectively Hire and Manage a Remote Team of Developers.
Download NowThe Mindbowser team's professionalism consistently impressed me. Their commitment to quality shone through in every aspect of the project. They truly went the extra mile, ensuring they understood our needs perfectly and were always willing to invest the time to...
CTO, New Day Therapeutics
I collaborated with Mindbowser for several years on a complex SaaS platform project. They took over a partially completed project and successfully transformed it into a fully functional and robust platform. Throughout the entire process, the quality of their work...
President, E.B. Carlson
Mindbowser and team are professional, talented and very responsive. They got us through a challenging situation with our IOT product successfully. They will be our go to dev team going forward.
Founder, Cascada
Amazing team to work with. Very responsive and very skilled in both front and backend engineering. Looking forward to our next project together.
Co-Founder, Emerge
The team is great to work with. Very professional, on task, and efficient.
Founder, PeriopMD
I can not express enough how pleased we are with the whole team. From the first call and meeting, they took our vision and ran with it. Communication was easy and everyone was flexible to our schedule. I’m excited to...
Founder, Seeke
Mindbowser has truly been foundational in my journey from concept to design and onto that final launch phase.
CEO, KickSnap
We had very close go live timeline and Mindbowser team got us live a month before.
CEO, BuyNow WorldWide
If you want a team of great developers, I recommend them for the next project. Â
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.Â
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
Mindbowser was great; they listened to us a lot and helped us hone in on the actual idea of the app. They had put together fantastic wireframes for us.
Co-Founder, Flat Earth
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
Founder, Child Life On Call
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork