以下是一个使用AngularJS计算匹配多个字符串的过滤结果数量的代码示例:
HTML模板:
- {{item}}
Total items: {{filteredItems.length}}
控制器:
angular.module('myApp', [])
.controller('myController', function($scope) {
$scope.items = ['apple', 'banana', 'cherry', 'date', 'elderberry'];
$scope.filteredItems = [];
$scope.$watch('search', function(newVal) {
$scope.filteredItems = $scope.items.filter(function(item) {
return item.toLowerCase().includes(newVal.toLowerCase());
});
});
});
在上面的示例中,我们使用了AngularJS的filter过滤器来过滤items数组中的元素。我们使用ng-model指令将搜索词绑定到search变量上,当search变量发生变化时,过滤器将自动重新运行。
在控制器中,我们使用$watch函数来监听search变量的变化。当search变量发生变化时,我们使用filter函数来过滤items数组,并将过滤后的结果存储在filteredItems数组中。
最后,我们在HTML模板中使用filteredItems.length来显示过滤结果的数量。每当搜索词发生变化时,AngularJS将自动更新显示的过滤结果数量。