首页 > 解决方案 > Angular 8:未在嵌套结构中显示 ng-tmplate 的内容

问题描述

我有一个模型,它的结构中有一个列表和一个枚举属性,如下所示:

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

我想用 ng-container 和 ng-template 在模板中显示我的模型列表,但是 ng-template 的内部内容没有显示在我的结果中我的模板是这样的:

<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>

你能帮助我吗?

标签: angularng-templatenebularng-container

解决方案


这是使用model.ItemsType而不是的基本错误model.itemtype

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

对于那些认为ng-template不能使用的人,请参阅为什么 *ngIf 不能与 ng-template 一起*ngIf使用的已接受答案?其中语法糖格式被指令替换(但不建议)。*ngIf[ngIf]


推荐阅读