我在模板中有一个条件,如下所示:

<ng-container>
    <p *ngFor="let seat of InfoDetails?.seatInfo">
        <template *ngIf="seat.section">
            Section {{seat?.section}} ,
        </template>
        <template *ngIf="seat.row">
            Row {{seat?.row}},
        </template>
        <template *ngIf="seat.seatNo">
            Seat number {{seat?.seatNo}}
        </template>
    </p>
</ng-container>

我有一个包含rowseatNo的数据集,但它似乎没有打印在模板中.这里有什么问题?

推荐答案

请阅读这里的文件,特别是

<div *ngIf="hero" >{{hero.name}}</div>

星号是表示更多的东西的"句法糖". 很复杂.在内部,角质脱糖分两个阶段进行.首先,它 翻译*ngIf="."转换为模板属性,template="ngIf .",像这样.

<div template="ngIf hero">{{hero.name}}</div>

然后将模板属性转换为

<ng-template [ngIf]="hero"> <div>{{hero.name}}</div></ng-template>

  • *ngIf指令移动到元素,在该元素中它成为属性绑定[ngIf].
  • 元素的其余部分(包括其class属性)在元素内部移动.

所以我们有ng-container

 <ng-container *ngIf="seat.section">
    Section {{seat.section}} ,
 </ng-container>

或者使用span或div或常规HTML标签.

 <span *ngIf="seat.section">
    Section {{seat.section}} ,
 </span>

或者如果您仍然想要使用ng-template(not recommended)

<ng-template [ngIf]="seat.section">
  Section {{seat.section}} ,
</ng-template>

Angular相关问答推荐

根据Angular中的构建来卸载独立组件

以Angular 表示显示特殊字符

从、管道和映射可观察到的逻辑?

类是一个独立的组件,不能在Angular中的`@NgModule.bootstrap`数组中使用

MonoTypeOperatorFunction不可分配给OperatorFunction类型的参数

如何让 ag-Grid 事件读取私有方法?

错误:服务或构建Angular 项目时模块构建失败

应该显示在表格中的嵌套循环

ng 生成组件不会创建 ngOnInit 和构造函数

如何更改 Angular material 按钮中的内部 RippleColor 和 UnboundedRipple 效果?

Angular2 路由 VS ui-router-ng2 VS ngrx 路由

在 Ionic 2 中使用 NodeJS.Timer 时找不到命名空间 NodeJS

初始化所有子类后的Angular 2生命周期钩子是什么?

如何以Angular 获取嵌套表单组的控件

angular 2 http withCredentials

Angular7 中的 CORS 策略已阻止源http://localhost:4200

Angular2中是否有像window.onbeforeunload这样的生命周期钩子?

提交后在Angular 2中重置表单

如何在 Angular 2 中动态添加和删除表单字段

Angular 2 中的动态模板 URL