角函数总是称为两次?

发布于 2025-02-07 14:41:12 字数 705 浏览 5 评论 0原文

在Angular I中,我在NGFOR指令的阵列上迭代。 HTML模板中的功能趋势始终称为两次。 有什么原因?

html-file

  <ul>
    <li *ngFor="let exercise of exercises">
      Name: {{exercise.name}}<br>      
      Sets: {{exercise.sets}}<span [innerHTML]="trend(exercise.sets,exercise.prev_sets)"></span>
    </li>
  </ul>

component.ts

trend(value: any, prev_value: any){
    console.log("Result:" + value);
    return "";
}

chrome console


Result: 12  exercises.component.ts:39 
Result: 12  exercises.component.ts:39

in Angular I iterate over an array with the ngFor directive.
The function trend in the HTML Template is called always twice.
Is there any reason why?

HTML-FILE

  <ul>
    <li *ngFor="let exercise of exercises">
      Name: {{exercise.name}}<br>      
      Sets: {{exercise.sets}}<span [innerHTML]="trend(exercise.sets,exercise.prev_sets)"></span>
    </li>
  </ul>

COMPONENT.TS

trend(value: any, prev_value: any){
    console.log("Result:" + value);
    return "";
}

CHROME CONSOLE


Result: 12  exercises.component.ts:39 
Result: 12  exercises.component.ts:39

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们的影子 2025-02-14 14:41:12

在每个更改检测周期中调用来自模板的方法/函数调用,

以避免这种情况绑定到属性或创建自定义管道。您也可以使用changeTectionstrategy.onpush减少更改检测检查

Method/function calls from the template are called on every change detection cycle

To avoid this either bind to a property or create a custom pipe. You can also use ChangeDetectionStrategy.onPush to reduce the change detection checks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文