I'm building a stepper and am using "transclusion" with ng-content to dynamically grab section elements within the stepper tag. The stepper.component view works like this:
<ng-content select=".one"></ng-content>
<ng-content select=".two"></ng-content>
<ng-content select=".three"></ng-content>
Component usage looks like this:
<stepper>
<section class="one">content here<section>
<section class="two">content here<section>
<section class="three">content here<section>
</stepper>
However, I'd like to make it dynamic by recognizing the section elements automatically:
<ng-content *ngFor="let section of sections; index as i;" select="['section:nth-child(' + i + ')']"></ng-content>
How can I:
- get a nodeList of the section elements available to transclude?
- use ng-content's
selectto target them incrementally?
I would create a directive like:
then add
stepper-sectionattribute to each of sections:and finally make use of
@ContentChildrendecorator to query all the sections:Ng-run Example
If you want to loop through content and render it dynamically you can wrap your children with
ng-templateand usengTemplateOutletdirective to render them in StepperComponent:html
stepper-section.directive.ts
stepper.component.ts
stepper.component.html
Ng-run Example
The difference between these two approaches is that in the first case all child content gets rendered and we only manipulate
display: noneto those steps we want to hide.In the second approach we have more control on what we want to render and we render only one step at specific time.