to check whether any value returned by filter in angular js

自古美人都是妖i 提交于 2019-12-25 08:48:42

问题


I have a view where a filter is being used filter: test_id.It is working absolutely fine.What I want to do is if the search filter does not return any data can I call a function in the controller at that instant of time when the filter returns null or no data in the list is shown as per search.

<input type="text" id="test_id" ng-model="test_id" value=""/>

<div class="test_list"> 
              <ul>
                <p> testID :{{test_id}}</p>-->
                 <li ng-repeat="test in test_list | filter: test_id">
                    {{test}}
                 </li>
              </ul>

         </div>

回答1:


Try this.

In your controller

$scope.filteredData = $scope.data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // dummy data

$scope.applyFilter = function () {
    $scope.filteredData = $scope.$eval('data | filter: filterWith');

    if (!$scope.filteredData || !$scope.filteredData.length) {
        // no-data. 
        // do your stuff.
    }
}

In your markup

<input type="text" ng-model="filterWith" ng-change="applyFilter()">
<div data-ng-repeat="item in filteredData" >
    {{item}}
</div>


来源:https://stackoverflow.com/questions/28766725/to-check-whether-any-value-returned-by-filter-in-angular-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!