gpt4 book ai didi

angularjs - 使用 Angular 过滤器搜索

转载 作者:行者123 更新时间:2023-12-01 16:24:16 24 4
gpt4 key购买 nike

引用下面的代码。

使用 Angular 过滤器我可以搜索 friend.name 或 friend.phone 或两者,但是如何在结合了两者的字符串中进行搜索,就像在下面的示例代码中我想搜索“mary - 80”并且它应该显示只有一个列表元素“Mary - 800-BIG-MARY”。

如何做到这一点,请帮助我。是否可以使用默认的 angularjs 过滤器??

<!doctype html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
</head>

<body ng-app="">
<div ng-init="friends = [{name:'John', phone:'555-1276'},
{name:'Mary', phone:'800-BIG-MARY'},
{name:'Mike', phone:'555-4321'},
{name:'Adam', phone:'555-5678'},
{name:'Julie', phone:'555-8765'},
{name:'Juliette', phone:'555-5678'}]"></div>

<label>Search: <input ng-model="searchText"></label>
<li ng-repeat="friend in friends | filter: searchText">
<span>{{friend.name}} - {{friend.phone}}</span>
</li>
</body>
</html>

相同代码的插件链接:http://plnkr.co/edit/p7valhnDulHorw8xDYu8?p=preview

最佳答案

这就是 angularJs 过滤器默认情况下的工作方式。如果要按特定的属性组合进行过滤,则必须实现自己的过滤功能。喜欢这里 https://scotch.io/tutorials/building-custom-angularjs-filters

super 简单的例子

  $scope.customFilter = (item)=> {
//TODO: Add your own properties here
return item.someProperty == $scope.filterValue;
};

html

<htmlElement ng-repeat="item in itemList | filter:customFilter"></htmlElement>

关于angularjs - 使用 Angular 过滤器搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31380775/

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