Javascript AngularJS按数组中的特定项筛选
我在角度1.4.8。我正在使用一个基本的搜索过滤器来查找用户。搜索工作正常,但正在搜索整个数据集。例如,在下面搜索“Mar”会找到三个结果,因为一个姓氏包含Mar。我只想按名字搜索。最终目标是找到找到的记录数:Result{{count}。所以搜索“St”应该会找到一个结果,第一个名字是史蒂夫(不是斯坦福) 这是一把小提琴: 您必须这样做:Javascript AngularJS按数组中的特定项筛选,javascript,angularjs,Javascript,Angularjs,我在角度1.4.8。我正在使用一个基本的搜索过滤器来查找用户。搜索工作正常,但正在搜索整个数据集。例如,在下面搜索“Mar”会找到三个结果,因为一个姓氏包含Mar。我只想按名字搜索。最终目标是找到找到的记录数:Result{{count}。所以搜索“St”应该会找到一个结果,第一个名字是史蒂夫(不是斯坦福) 这是一把小提琴: 您必须这样做: $scope.items = $filter('filter')($scope.items2, {firstname:val}); 就这些。您必须这样做
$scope.items = $filter('filter')($scope.items2, {firstname:val});
就这些。您必须这样做:
$scope.items = $filter('filter')($scope.items2, {firstname:val});
就这些。您必须编写($scope.items2,{firstname:val})代码>
您可以使用以下代码解决您的问题
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', ['$scope', '$filter', function($scope, $filter) {
$scope.items = [{
id: 1,
firstname: 'John',
lastname: 'Jones'
}, {
id: 2,
firstname: 'Steve',
lastname: 'Jones'
}, {
id: 3,
firstname: 'Joey',
lastname: 'Maritza'
}, {
id: 4,
firstname: 'Mary',
lastname: 'Linqust'
}, {
id: 5,
firstname: 'Marylin',
lastname: 'Stanford'
}];
$scope.items2 = $scope.items;
$scope.$watch('search', function(val) {
$scope.items = $filter('filter')($scope.items2, {firstname:val});
$scope.count = $scope.items.length;
});
}]);
您必须编写($scope.items2,{firstname:val})代码>
您可以使用以下代码解决您的问题
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', ['$scope', '$filter', function($scope, $filter) {
$scope.items = [{
id: 1,
firstname: 'John',
lastname: 'Jones'
}, {
id: 2,
firstname: 'Steve',
lastname: 'Jones'
}, {
id: 3,
firstname: 'Joey',
lastname: 'Maritza'
}, {
id: 4,
firstname: 'Mary',
lastname: 'Linqust'
}, {
id: 5,
firstname: 'Marylin',
lastname: 'Stanford'
}];
$scope.items2 = $scope.items;
$scope.$watch('search', function(val) {
$scope.items = $filter('filter')($scope.items2, {firstname:val});
$scope.count = $scope.items.length;
});
}]);