......">

Using ngIf without an additional element in Angular 2

Can I use ngIf without an extra container element?

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

It does not work in the table because it will make the HTML incorrect.

+68
html angular templates
Apr 02 '16 at 16:46 on
source share
3 answers

ng-container preferred over template :

 <ng-container *ngIf="expression"> 

Cm:

Angular 2 ng-container

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

+98
Dec 20 '16 at 4:46
source share

I found a method for this: https://angular.io/docs/ts/latest/guide/template-syntax.html#!#star-template .

You can simply use the <template> and replace *ngIf with [ngIf] as follows.

 <template [ngIf]="..."> ... </template> 
+19
Apr 03 '16 at 7:57
source share

You cannot put a div directly inside tr , which will result in invalid HTML. tr can only contain the td / th / table element, and there may be other HTML elements inside them.

You can slightly modify your HTML to have *ngFor over tbody and have ngIf on tr , as shown below.

 <tbody *ngFor="..."> <tr *ngIf="..."> ... </tr> <tr *ngIf="!..."> ... </tr> .. </tbody> 
+4
Apr 02 '16 at 17:22
source share



All Articles