ng-container
is preferred over template
:
<ng-container *ngIf="expression">
See:
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>
template
tag ,by default angular directives prefix with * creates an template tag. so both are same [ngIf] and *ngIf
*ngIf
you have an element inside the template, you do not if you write the template
yourself. Under certain circumstances the extra element could interfere.
template
tag inside, tr
/td
tag?
*ngIf="foo"
is equivalent to the wrapping <template [ngIf]="foo">
tag. In short, template
+ []
== *
, so []
!= *
. *
makes sense in any element except 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>
adding brackets resolves this issue
<ng-container *ngIf="(!variable| async)"></ng-container>
You can try this:
<ng-container *ngFor="let item of items;">
<tr *ngIf="item.active">
<td>{{item.name}}</td>
</tr>
</ng-container>
Here, I have iterate loop in ng container so it will not create extra dom and later in tr tag check condition if I want to render or not.
Success story sharing
ng-container
seems to generate adiv
in our case?