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

时间:2015-04-13 14:53:56

标签: angularjs function ng-repeat

我有这段代码:

 <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次? 谢谢!

1 个答案:

答案 0 :(得分:1)

您可以使用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">