以下是一个使用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将自动更新显示的过滤结果数量。