ng-repeat - 是否可以存储函数调用的结果?

ng-repeat - Is it possible to store result of function call?

我有这个代码:

 <tbody ng-repeat="term in Items | filter:searchText track by term.Element">
                        <tr class="term-row">

                            <td ng-show="!IsValid(term)" class="text-center">
                                <img ng-show="GetStatus(term)==''" src='@Url.Content("~/Lib/img/spinner26x26.gif")' />
                                <span class="label label-danger">Invalid</span>
                            </td>
                            <td ng-show="IsValid(term)" class="text-center">
                                <img ng-show="GetStatus(term)==''" src='@Url.Content("~/Lib/img/spinner26x26.gif")' />
                                <span class="label label-success" ng-bind="GetStatus(term)"></span>
                            </td>

                        </tr>

如您所见,我在遍历转发器时在多个位置调用了 IsValid(item)。是否可以将该方法的结果存储在转发器变量的本地中,然后使用该结果而不是调用函数 x 次? 谢谢!

您可以使用 ng-init:

<tbody ng-repeat="term in Items | filter:searchText track by term.Element" ng-init="isValid = IsValid(term)">

然后使用那个值

<td ng-show="!isValid" class="text-center">
<td ng-show="isValid" class="text-center">