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
janispritzkau Apr 02 '16 at 16:46 on 2016-04-02 16:46
source share3 answers
ng-container preferred over template :
<ng-container *ngIf="expression"> Cm:
Angular 2 ng-container
+98
Alexander Taylor Dec 20 '16 at 4:46 2016-12-20 04:46
source shareI 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
janispritzkau Apr 03 '16 at 7:57 2016-04-03 07:57
source shareYou 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
Pankaj Parkar Apr 02 '16 at 17:22 2016-04-02 17:22
source share