Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to refactor ngFor when element uses the iteration variable on the element?

I have this

<div
    *ngFor="let dt of totals"
    [ngClass]="{ up: dt.isUp }"
>
    <span> {{ dt }}</span>
</div>

and I want to refactor to angular v17 syntax

@for (dt of totals; track $index) {
    <span>{{ dt }}</span>
}

How do I refactor the [ngClass] on the <div>?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried this but obviously It doesn’t work because the variable dt is not yet defined inthe <div>

<div [ngClass]="{ up: dt.isUp }">
    @for (dt of totals; track $index) {
        <span>{{ dt }}</span>
    }
</div>

I could try this but I don’t want an extra ng-container on every element:

<div>
    @for (dt of totals; track $index) {
        <ng-container [ngClass]="{ up: dt.isUp }">
            <span>{{ dt }}</span>
        </ng-container>
    }
 </div>

What’s the official way to do this?

>Solution :

In your example you are creating multiple divs, by using the *ngFor so the equivalent of the same will be the below code.

The line where you place the *ngFor get repeated.

@for (dt of totals; track $index) {
    <div [ngClass]="{ up: dt.isUp }" >
        <span> {{ dt }}</span>
    </div>
}

If you do not want the div tag, then you can add the [ngClass] to the span, the ngClass must be attached to an HTML element. Since the class is present in html, we cannot use ng-container because there is no tag present in the HTML during rendering!

@for (dt of totals; track $index) {
    <span [ngClass]="{ up: dt.isUp }"> {{ dt }}</span>
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading