Angular 的 <ng-container> 是一个分组元素,它不会干扰样式或布局,因为 Angular 不会将其放置在 DOM 中。

为没有DOM元素得指令安排宿主

当没有单个元素承载指令时,可以使用 <ng-container>

//使用 <ng-container> 的条件化段落
<p>I turned the corner
  <ng-container *ngIf="hero">and saw {{hero.name}}. I waved </ng-container> and continued on my way.
</p>
//要有条件地排除 <option>,请将 <option> 包裹在 <ng-container> 中
<select [(ngModel)]="hero">
  <ng-container *ngFor="let h of heroes">
    <ng-container *ngIf="showSad || h.emotion !== 'sad'">
      <option [ngValue]="h">{{h.name}} ({{h.emotion}})</option>
    </ng-container>
  </ng-container>
</select>
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐