Search code examples
angularcheckboxangular2-changedetectionangular-changedetection

Angular checkbox state check fires too often


In my UI, I have too many checkboxes arranged in the form of a grid. Their checked status should be determined from the one time logic which is present in the component. Once updated their status are never going to get changed.

I have updated the checked status by calling a function as below.

[checked]="getCheckedStatus()"

Simplified problem is present in this stackblitz - https://stackblitz.com/edit/angular-o622bw?embed=1&file=src/app/app.component.html

Problem - console.log() (or say getCheckedStatus()) gets fired too often whenever Update button is clicked which is slowing down the performance.


Solution

  • I think that you problem is calling a function from the view. This makes Angular call the function every type it checks the view. So calling function from the view can be a performance problem. If you need to transform thing based on things in the view you can use a pipe. Now it will not be called every time, and is handled by Angular.

    I hope it's ok but I took a copy of you stackbliz and made a version using a pipe called selectStatus. [checked]="{accnt:accnt, itm:itm} | selectStatus"

    https://stackblitz.com/edit/angular-hzdt4n