Count: {{$ctrl.filteredOperations .length}} ... 我-6ren">
gpt4 book ai didi

javascript - 将过滤后的数据分配给 ng-if 中的 Controller 变量

转载 作者:行者123 更新时间:2023-12-01 03:57:20 25 4
gpt4 key购买 nike

我想将过滤后的数据分配给 ng-if 中的 Controller 变量:

<section ng-if="($ctrl.filteredOperations = $ctrl.operations | filter: customFilter).length > 0">
<header>Count: {{$ctrl.filteredOperations .length}}</header>
<div ng-repeat="operation in $ctrl.filteredOperations ">...</div>
</section>

我尝试了不同的方法,但没有成功。

更新

我为什么要这样做?

我有一项保存操作集合的服务。

financialService.operations(); - 这会从 API 或缓存返回操作。

所以在我的 Controller 中:

financialService.operations().then(function(ops){
vm.operations = ops;
});

现在,如果我以任何方式更新我的缓存,它就会更新我的 View 。

最佳答案

我建议您将过滤移至 Controller :

$ctrl.filteredOperations = $filter('customFilter')($ctrl.operations);

然后显示如下:

<section ng-if="$ctrl.filteredOperations.length">
<header>Count: {{$ctrl.filteredOperations.length}}</header>
<div ng-repeat="operation in $ctrl.filteredOperations">...</div>
</section>
<小时/>

在我看来,当逻辑变得复杂时,最好将其移至 Controller ,而不是全部在 HTML 中完成。

关于javascript - 将过滤后的数据分配给 ng-if 中的 Controller 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42535500/

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