问题
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