json AngularJs,过滤不区分大小写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18082017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJs, filter case insensitive
提问by user2462805
I've got an json full of datas with lowcase and upcase. For example :
我有一个充满小写和大写数据的json。例如 :
[
{ "firstName":"JoHn" , "lastName":"DoE" },
{ "firstName":"aNnA" , "lastName":"smIth" },
{ "firstName":"PeTer" , "lastName":"JOnes" }
]
And I've got something similar to this :
我有类似的东西:
Search: <input ng-model="searchText">
<table id="searchTextResults">
<tr><th>Name</th><th>Phone</th></tr>
<tr ng-repeat="friend in friends | filter:searchText">
<td>{{friend.firstName}}</td>
<td>{{friend.lastName}}</td>
</tr>
</table>
What I want to do is to search a friend without looking at upcases and lowcases. So basically when I type "John", "JOHN" or simply "john" in my input, it should return my friend John.
我想要做的是在不查看大写和小写的情况下搜索朋友。所以基本上当我在输入中输入“John”、“JOHN”或简单的“john”时,它应该返回我的朋友约翰。
So is it possible to apply a case insensitive option to a filter ?
那么是否可以将不区分大小写的选项应用于过滤器?
采纳答案by Steve Kl?sters
Pass a function name to filterwhich you define on an applicable scope, where you use string's toLowerCase. See ngFilter. The function signature is a simple function (item) { ... }.
传递filter您在适用范围内定义的函数名称,您在其中使用字符串的toLowerCase。参见ngFilter。函数签名是一个简单的function (item) { ... }.
Example JS in your controller:
控制器中的示例 JS:
$scope.filterOnlyFoo = function (item) {
return item == "foo";
}
Example HTML:
示例 HTML:
<div data-ng-repeat="item in items | filter: filterOnlyFoo"></div>

