gpt4 book ai didi

javascript - ng-repeat 一次性运行多次?

转载 作者:行者123 更新时间:2023-12-03 04:25:35 27 4
gpt4 key购买 nike

我的理解很简单

<table>
<tr>
<td ng-repeat="c in schedIdTableCtrl.calculateNumberOfColumns() track by $index">{{schedIdTableCtrl.calculateTime($index)}}</td>
</tr>
</table>

它返回一个包含 504 个元素的数组。

但是当我 console.log()它说

(2) 504
(2) 504
504

那么它不是只运行一次,而是运行五次?

我的 JavaScript 很简单

ctrl.calculateNumberOfColumns = () => {
let columns = length / interval;
console.log(columns);
return new Array(columns);
};

最佳答案

这是正常行为,与 Angular $digest 循环相关。

This cycle can be considered as a loop, during which Angular checks if there are any changes to all the variables watched by all the scopes. So if you have $scope.myVar defined in your controller and this variable was marked for being watched, then you are explicitly telling Angular to monitor the changes on myVar in each iteration of the loop. Source

如果可以仅在 Angular 中绑定(bind)变量一次,请使用 ::。请参阅this answer .

关于javascript - ng-repeat 一次性运行多次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43753520/

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