我想将过滤后的数据分配给 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/