Reputation: 49
I have a problem with Anguar2 and ngIf:
I have a code that creates a table from an array(with a DIY coded offset of 6):
<table class="table2">
<tr>
<th>Raum</th>
<th>Ticket</th>
</tr>
<tr *ngFor="let a of aufrufe | async; let odd = odd; let i = index" [@newsState]="anistate[a.appid]" (click)="switchState(a)">
<ng-container *ngIf="a.room != 'Beratungsplatz' && i > 7 ">
<ng-container *ngIf="odd">
<td class="roomodd">{{a.room}}</td>
<td class="ticketodd">{{a.ticket}}</td>
</ng-container>
<ng-container *ngIf="!odd">
<td class="room">{{a.room}}</td>
<td class="ticket">{{a.ticket}}</td>
</ng-container>
</ng-container>
</tr>
</table>
The problem is, that angular creates an empty tr with this comment in it:
<tr _ngcontent-c1="" class="">
<!--bindings={
"ng-reflect-ng-if": "false"
}-->
</tr>
And that "destroys" my style. Shouldn't the ngIf in the first ng-container print nothing if it it false?(index > 7)
Thank you in advance!
Upvotes: 4
Views: 8542
Reputation: 8332
Obvious answer would be to put *ngIf
and *ngFor
on <tr>
but that is not possible.
My proposal is to make pipe that will filter all records that fall under condition a.room != 'Beratungsplatz' && i > 7
. That way you will print only elements that need to be there.
Example:
Pipe
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'rooms'
})
export class RoomsFilter implements PipeTransform {
transform(value: any, roomName: string, index: number, allowedValue: number ): any {
return room != roomName && index > allowedValue ;
}
}
Html
...
<tr *ngFor="let a of aufrufe | async | rooms: a.room: 'Beratungsplatz': i : 7; let odd = odd; let i = index" [@newsState]="anistate[a.appid]" (click)="switchState(a)">
<ng-container *ngIf="odd">
<td class="roomodd">{{a.room}}</td>
<td class="ticketodd">{{a.ticket}}</td>
</ng-container>
<ng-container *ngIf="!odd">
<td class="room">{{a.room}}</td>
<td class="ticket">{{a.ticket}}</td>
</ng-container>
</tr>
...
Upvotes: 0
Reputation: 2701
Try with template
<table class="table2">
<tr>
<th>Raum</th>
<th>Ticket</th>
</tr>
<template ngFor let-a [ngForOf]="aufrufe | async" let-i=index [@newsState]="anistate[a.appid]" (click)="switchState(a)">
<tr *ngIf="a.room != 'Beratungsplatz' && i > 7 ">
<ng-container>
<ng-container *ngIf="odd">
<td class="roomodd">{{a.room}}</td>
<td class="ticketodd">{{a.ticket}}</td>
</ng-container>
<ng-container *ngIf="!odd">
<td class="room">{{a.room}}</td>
<td class="ticket">{{a.ticket}}</td>
</ng-container>
</ng-container>
</tr>
</template>
</table>
Upvotes: 0
Reputation: 3099
Try using <ng-container>
for the *ngFor
, and only include the <tr>
when you are inside the first *ngIf
<ng-container *ngFor="let a of aufrufe | async; let odd = odd; let i = index" >
<ng-container *ngIf="a.room != 'Beratungsplatz' && i > 7 ">
<tr [@newsState]="anistate[a.appid]" (click)="switchState(a)">
<ng-container *ngIf="odd">
<td class="roomodd">{{a.room}}</td>
<td class="ticketodd">{{a.ticket}}</td>
</ng-container>
<ng-container *ngIf="!odd">
<td class="room">{{a.room}}</td>
<td class="ticket">{{a.ticket}}</td>
</ng-container>
</tr>
</ng-container>
</tr>
Upvotes: 2