在Angular 2中使用没有额外元素的ngIf

jan*_*kau 93 html templates angular

ngIf没有额外的容器元素我可以使用吗?

<tr *ngFor="...">
  <div *ngIf="...">
    ...
  </div>
  <div *ngIf="!...">
    ...
  </div>
</tr>
Run Code Online (Sandbox Code Playgroud)

它在表中不起作用,因为这会产生无效的HTML.

Ale*_*lor 144

ng-container优先于template:

<ng-container *ngIf="expression">
Run Code Online (Sandbox Code Playgroud)

看到:

角度2 ng容器

https://github.com/angular/angular.io/issues/2303

  • 我每天来这里两次。我无法把它牢记在心。 (4认同)

jan*_*kau 19

我找到了一种方法:https://angular.io/docs/ts/latest/guide/template-syntax.html#!# star-template.

你可以简单地使用<template>标签和替换*ngIf[ngIf]这样的.

<template [ngIf]="...">
  ...
</template>
Run Code Online (Sandbox Code Playgroud)