gpt4 book ai didi

javascript - AngularJS 按数组中的特定项目进行过滤

转载 作者:行者123 更新时间:2023-11-28 14:48:30 26 4
gpt4 key购买 nike

我使用的是 Angular 1.4.8。我正在使用基本搜索过滤器来查找用户。搜索有效,但正在搜索整个数据集。例如,在下面搜索“Mar”将找到三个结果,因为其中一个姓氏包含 Mar。我只想按名字搜索。最终目标是找到找到的记录数:结果{{count}}。因此,搜索“St”应该找到一个结果,即第一个名字 Steve(不是斯坦福大学)。

这是一个 fiddle : http://jsfiddle.net/mediaguru/z5shgzxw/1/

  <div ng-app="myApp">
<div ng-controller="MyCtrl">
<input type="text" ng-model="search">
<table>
<tbody>
<tr ng-repeat="item in items">
<td>ID: {{item.id}}</td>
<td>Name: {{item.firstname}}</td>
<td>Gender: {{item.lastname}}</td>
</tr>
</tbody>
</table>
Result {{count}}
</div>
</div>

JS

  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, val);
$scope.count = $scope.items.length;
});


}]);

最佳答案

您必须这样做:

$scope.items = $filter('filter')($scope.items2, {firstname:val});

这就是全部。

关于javascript - AngularJS 按数组中的特定项目进行过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45470337/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com