I am trying to loop some items with an ngFor and create a mat-expansion-panel for each item.
I would like to encapsulate the logic for each item into an item component:
<item></item>
Initial attempt:
<mat-accordion>
<div *ngFor="let item of items" >
<item [item]="item"></item>
</div>
</mat-accordion>
Where item template is:
<mat-expansion-panel>
<mat-expansion-panel-header>
Header Content
</mat-expansion-panel-header>
<div>
Expandable content
</div>
</mat-expansion-panel>
The problem with this approach is that there is an extra html element for my component between <mat-accordion>
and <mat-expansion-panel>
, which messes up the css for the accordion.
Is there a way that my component can provide the header and content?
<mat-accordion>
<div *ngFor="let item of items" >
<mat-expansion-panel>
<mat-expansion-panel-header>
<!-- put item component header here->
</mat-expansion-panel-header>
<div>
<!-- put item component content here->
</div>
</mat-expansion-panel>
</div>
</mat-accordion>
I have read up on ng-content, but I think that is backwards from what I want, I don't want to shove custom content into my component, I want to extract elements from my component and have them render in the parent.
I do realize I could create 2 components, item-header and item-content. However I would really like to keep that login in one component.
with for repeats the- within. But haven’t tried on mat-accordian and been long day so could be wrong!
– JMP Feb 28 '20 at 18:49