angular.js - Angular 用filter过滤ng-repeat循环之后的数组,如何知道过滤后的数组是多少?

查看:178
本文介绍了angular.js - Angular 用filter过滤ng-repeat循环之后的数组,如何知道过滤后的数组是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

或者是换个提问方式,如何知道ng-repeat输出渲染的数组是多少个?

我在官方文档找到一个疑似可以用的东西,但是无奈英文太差,谁帮我看看怎么运用?
AngularJS-ngRepeat

翻到网页最后,可以看到For example: item in items | filter:x as results will store the fragment of the repeated items as results, but only after the items have been processed through the filter.

例子:

 <ul class="example-animate-container">
      <li class="animate-repeat" ng-repeat="friend in friends | filter:q as results">
        [{{$index + 1}}] {{friend.name}} who is {{friend.age}} years old.
      </li>
      <li class="animate-repeat" ng-if="results.length == 0">
        <strong>No results found...</strong>
      </li>
    </ul>
    

如代码举例,ng-repeat="friend in friends | filter:q as results"results能直接得到筛选后的数组,results.length即筛选后的数组长度,但是我用了这个方法,发现有个问题,在HTML中直接输出results能得到数据,但是在controller中的$scope.results是空的,请问如何在controller中获取到该results

解决方案

假设你 view 里是这样的

arr | filter1

那么在 controller 中你可以这样

angular.module("app").controller("controller", ['$scope', 'filter1', function($scope, filter1){
    var len = filter1($scope.arr).length;
}]);

我觉得这个方法是可以的

这篇关于angular.js - Angular 用filter过滤ng-repeat循环之后的数组,如何知道过滤后的数组是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆