Using ngIf without an extra element in Angular 2
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.
ng-container
is preferred over template
:
<ng-container *ngIf="expression">
See:
Angular 2 ng-container
https://github.com/angular/angular.io/issues/2303
I found a method for that on: https://angular.io/docs/ts/latest/guide/template-syntax.html#!#star-template.
You can simply use the <template>
tag and replace *ngIf
with [ngIf]
like this.
<template [ngIf]="...">
...
</template>
You can't put div
directly inside tr
, that would make invalid HTML. tr
can only have td
/th
/table
element in it & inside them you could have other HTML elements.
You could slightly change your HTML to have *ngFor
over tbody
& have ngIf
over tr
itself like below.
<tbody *ngFor="...">
<tr *ngIf="...">
...
</tr>
<tr *ngIf="!...">
...
</tr>
..
</tbody>