Search code examples
angularng-templatenebularng-container

Angular 8: Not Showing the content of ng-tmplate in nested structure


I have a model that in its structure has a list and one enum property like this:

export class myModel {
    Items: any[]=[];
    ItemsType: ItemType;
}

and I want to show the list of my model in the template with ng-container and ng-template but inner content of ng-template not showing in my result my template is like this:

<nb-accordion>
  <ng-container *ngFor="let model of myModels">
    <ng-template [ngIf]="model.itemType == 0">
      <nb-accordion-item *ngFor="let item of model.Items">
        <nb-accordion-item-header>
          ...some content
        </nb-accordion-item-header>
        <nb-accordion-item-body>
          ...some content
        </nb-accordion-item-body>
      </nb-accordion-item>
    </ng-template>
    <ng-template [ngIf]="model.itemType == 1">
      <nb-accordion-item *ngFor="let item of model.socialNetworkItems">
        <nb-accordion-item-header>
          ...some content
        </nb-accordion-item-header>
        <nb-accordion-item-body>
          ...some content
        </nb-accordion-item-body>
      </nb-accordion-item>
    </ng-template>
  </ng-container>
</nb-accordion>

can you help me?


Solution

  • It is a basic mistake of using model.ItemsType instead of model.itemtype

    StackBlitz demo: https://stackblitz.com/edit/angular-nested-template-reference

    For those who are thinking that ng-template cannot be used with *ngIf see the accepted answer at why *ngIf doesnt'work with ng-template? where the syntactical sugar format *ngIf is replaced with [ngIf] directive (but is not advised).