0

I have a function that is called for each iteration.

And not excpectedly, this function is called twice at each iteration.

public setLevel(deviceId: string, averageFps: number): void {
    let device = `device-${deviceId}`;

    // (<HTMLElement>document.querySelector(`#${device} .level-fps-wrapper .level-fps`)).style.width = `${averageFps / 300 * 100}%`;
    console.log(device);
}

Here is the html template

<div class="container">
<div class="row">
    <h1 class="header">Devices</h1>
    <div class="col l4 m6 s12 clickable" *ngFor="let device of this.devices" (click)="goToDevice(device)">
        <div class="card" [id]="'device-' + device.device._id">
            <div class="card-content">
                <div class="row no-margin-bottom">
                    <div class="col s2">
                        <img class="circle circle-element" [src]="'./app/devices/images/' + device.device.os + '_logo.png'" alt="">
                    </div>
                    <div class="col s10">
                        <span class="card-title">{{ device.device.name }}</span>
                        <p>
                            {{ device.averageFps | number: '.1-2' }} fps
                        </p>
                    </div>
                </div>
            </div>
            <div class="card-action level-fps-wrapper">
                <div class="level-fps">{{ this.setLevel(device.device._id, device.averageFps) }}</div>
            </div>
        </div>
    </div>
</div>

This is the output

This is the output

It should be 3 lines instead of 6.

Someone could explain it to me? Is there something to do with angular Lifecycle hooks?

Thanks for your answers!

Yoann Picquenot
  • 640
  • 10
  • 26
  • 2
    Angular will rebind all of your bindings on every change loop. This occurs, quite often, and will happen twice as much while in developer mode, as it runs a loop again after running the first loop to make sure variable haven't been changed inside the change loop. I would not recommend placing any logic inside these types of bindings, and instead use them to simply reflect the state of a variable, with your logic happening somewhere else in the code. https://stackoverflow.com/questions/34465301/in-angular2-why-are-there-2-times-check-for-content-and-view-after-settimeout – LarsMonty Aug 10 '17 at 17:47

1 Answers1

0

Yes, this is quite usual. Similar to angular 1 there is no way that angular can tell whether the result of the function will have changed, so it calls it on every digest loop.

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396