-6ren">
gpt4 book ai didi

javascript - Angularjs 1.5x : How to call a function only once in a template and reuse the result in multiple places

转载 作者:行者123 更新时间:2023-11-27 22:50:32 25 4
gpt4 key购买 nike

我在每个部分中都留下了一些问题,并且该计数会向用户显示,除非剩下零个问题,在这种情况下我会显示一个复选标记。

<p class="list-group-item-heading">{{section.title}}
<i ng-show="questionsLeft(section)==0" class="fa fa-check"></i>
<span ng-show="questionsLeft(section)>0" class="label">
{{questionsLeft(section)}}</span>
</p>

函数 questionsLeft() 被调用了 3 次 - 如何调用一次并重用其结果?我已经尝试过 ng-init 和 {{x = questionsLeft(section); ""}} 欺骗这两个都初始化变量,但当值更改时它不会更新。

最佳答案

  1. section 组件 Controller 内创建一个变量。
  2. 将此变量而不是模板中的函数放入其中。
  3. 每次问题数量发生变化时,使用 questionsLeft 函数更新变量值。例如,您可以在 $onChanges Hook 内执行此操作。

附注如果你提供 Controller 代码会更容易说

关于javascript - Angularjs 1.5x : How to call a function only once in a template and reuse the result in multiple places,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38094500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com