Angular ng-repeat item过滤后计数

时间:2015-03-22 05:45:38

标签: javascript angularjs

过滤后如何获取ng-repeat中的项目数?

但是当我使用@ {{requestlist.length}}代码时,我得到了记录的总数。而不是我需要按类别和文本搜索过滤后的记录数。

请帮帮我

HTML

    <input type="text" placeholder="Search" ng-model="search">

    <label class="checkbox" for="checkboxes-@{{subcatagory.id}}" ng-repeat="subcatagory in subcategories">
        <input name="checkboxes" id="checkboxes-@{{subcatagory.id}}"   ng-click="includeCatagory(subcatagory.id)" type="checkbox">
        @{{subcatagory.titel}}
    </label>

    <div class="eol-layout-s3" ng-repeat="request in requestlist | filter:catagoryFilter | filter:search | orderBy:'titel':true">
        <div id="jobDesc-bd">
            <a href="/request/@{{request.id}}" >
                <strong>
                @{{request.titel | characters:30}} 
                </strong>
            </a>

            <small>
                @{{request.message | characters:200}}  
            </small>
        </div>
    </div>
<h2>count: @{{requestlist.length}}</h2>

$http.get('/api/subcategories/{{$seller->category_id}}/').success(function(subcategories){
        $scope.subcategories = subcategories;
    });
    $http.get('/api/requests/list/').success(function(requests){
        $scope.requestlist = requests;
    });

    $scope.catagoryFilter = function(cat) {
                if ($scope.catagoryIncludes.length > 0) {
                    if ($.inArray(cat.subcategorie_id, $scope.catagoryIncludes) < 0)
                        return;
                }

                return cat;
            }

1 个答案:

答案 0 :(得分:5)

修改

ng-repeat="request in requestlist 
               | filter:catagoryFilter | filter:search | orderBy:'titel':true"

ng-repeat="request in (filterResult = (requestlist | filter:catagoryFilter 
| filter:search | orderBy:'titel':true))"

长度可以通过模板中的{{filterResult.length}}或控制器中的$scope.filterResult.length来访问。

注意:您可能希望初始化$scope.filterResult = [];有时会出现未定义的错误。

请注意,您可以通过在过滤器中将结果分配到某个变量中来存储过滤结果,如上例(filterResult)中所示。然后,您可以使用此模型"filterResult"。即使在控制器$scope.filterResult中也可以使用。