0

Use Angular 2

When i do it:

<ul>
    <template *ngFor="let flag of [1,2,3,4]">
        <li  *ngIf="flag == 1">
            some data
        </li>
    </template>
</ul>

I get it

<ul>
    <!--template bindings={
      "ng-reflect-ng-for-of": "1,2,3,4"
    }-->
    <!--template bindings={}-->
    <!--template bindings={}-->
    <!--template bindings={}-->
    <!--template bindings={}-->
</ul>

And it display nothing. I need to display <li> in *ngFor only when item.flagsExplain[flag] is set

Denis Ostrovsky
  • 519
  • 1
  • 6
  • 16

1 Answers1

1

for syntactic sugar:

<ul>
    <ng-container*ngFor="let flag of service.flagsCount()">
        <li  *ngIf="item.flagsExplain[flag]">
            {{ service.flagLabel(flag) }}
            <b *ngIf="item.flagsExplain[flag] != 1">
                : {{ item.flagsExplain[flag] }}
            </b>
        </li>
    </ng-container>
</ul>

if you wanna using template tag, you need using this form:

<template ngFor let-item [ngForOf]="items" let-i="index" [ngForTrackBy]="trackByFn">
  <li>...</li>
</template>

and in Angular 4:

<ng-template ngFor let-item [ngForOf]="items" let-i="index" [ngForTrackBy]="trackByFn">
  <li>...</li>
</ng-template>

so your code will look like this:

<ul>
    <template ngFor let-flag [ngForOf]="service.flagsCount()">
        <li  *ngIf="item.flagsExplain[flag]">
            some data
        </li>
    </template>
</ul>

p/s: replace template tag with ng-template in Angular 4.

document here: https://angular.io/docs/ts/latest/api/common/index/NgForOf-directive.html

Tiep Phan
  • 12,386
  • 3
  • 38
  • 41