I have an instance of ngb-carousel which is currently used like this:
<ngb-carousel>
<ng-template ngbSlide>Hello</ng-template>
<ng-template ngbSlide>World</ng-template>
</ngb-carousel>
I would like to wrap it into my custom carousel component providing some extra features, to use it like this:
<!-- my-carousel.component.html -->
<div class="my-carousel">
<ngb-carousel>
<ng-content></ng-content>
</ngb-carousel>
<div>Some text here</div>
</my-carousel>
<!-- usage -->
<my-carousel>
<ng-template ngbSlide>Hello</ng-template>
<ng-template ngbSlide>World</ng-template>
</my-carousel>
However, this naive approach does not work, as the slides are not being displayed.
I have tried to use <ng-content select="ng-template[ngbSlide]" ngProjectAs="ng-template[ngbSlide]"> and other combinations to no avail.
What am I missing? Which is the proper way to do it?
We need to create templates with a template reference
slide.Then we can take a
ContentChildrento get all the slides through content projectionafter getting the slides, the key is to use a
ng-templatewith the attributengbSlidefor the ngb-carousel to recognize it as a slideWe can use
@forto loop through the slides and render them inside ang-templateusingngTemplateOutletWorking example below
parent html
parent ts
my carousel ts
stackblitz