ngFor循环上的函数被调用太多次 [英] Function on ngFor loop is called too many times

查看:348
本文介绍了ngFor循环上的函数被调用太多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在循环中有一个复选框组件,它调用控制器以查看它是否在初始加载时被检查。它循环的列表只有三个项目,但该函数被调用了24次。

I have a checkbox component in a loop that makes a call to the controller to see if it's checked on initial load. The list that it's looping through is only three items long but the function is being called 24 times.

HTML:

       <div class="MhSearchForm__Checkboxes">
        <div *ngFor="let propertyStatus of propertyStatuses" class="ml-3">
          <app-mh-check-box [option]="propertyStatus" [checked]="isChecked(propertyStatus)" (action)="statusSelected($event)"></app-mh-check-box>
        </div>
      </div>

JS:

  isChecked(status) {
   console.log(status);
  }


推荐答案

这是正常的,它是由于Angular每次模型更改时都会调用该函数,因为它不知道模型更改是否具有预期和正常的影响。

This is Normal, It is due to the fact that Angular will call that function every time the model changes, because it has no idea if the model change has an impact That's expected and normal.

这篇关于ngFor循环上的函数被调用太多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆