Using ngIf without an extra element in Angular 2

janispritzkau picture janispritzkau · Apr 2, 2016 · Viewed 47.6k times · Source

Can I use ngIf without an extra container element?

<tr *ngFor="...">
  <div *ngIf="...">
    ...
  </div>
  <div *ngIf="!...">
    ...
  </div>
</tr>

It doesn't work in a table because that would make invalid HTML.

Answer

Alexander Taylor picture Alexander Taylor · Dec 20, 2016

ng-container is preferred over template:

<ng-container *ngIf="expression">

See:

Angular 2 ng-container

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