AngularJs,过滤器不区分大小写 [英] AngularJs, filter case insensitive

查看:1857
本文介绍了AngularJs,过滤器不区分大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON充满lowcase和upcase DATAS的。
例如:

I've got an json full of datas with lowcase and upcase. For example :

[

  { "firstName":"JoHn" , "lastName":"DoE" }, 
  { "firstName":"aNnA" , "lastName":"smIth" }, 
  { "firstName":"PeTer" , "lastName":"JOnes" }

]

和我有类似这样的:

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>

我想要做的就是寻找一个朋友不看upcases和lowcases。
所以基本上,当我输入约翰,约翰或简称为约翰在我的输入,它应该返回我的朋友约翰。

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.

那么,它可以应用不区分大小写的选项来过滤?

So is it possible to apply a case insensitive option to a filter ?

推荐答案

传递一个函数名称过滤用于定义上的适用范围,在您使用字符串的<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase\">toLowerCase.请参见 ngFilter 。函数签名是一个简单的函数(项目){...}

Pass a function name to filter which you define on an applicable scope, where you use string's toLowerCase. See ngFilter. The function signature is a simple function (item) { ... }.

JS例子在你的控制器:

Example JS in your controller:

$scope.filterOnlyFoo = function (item) {
    return item == "foo";
}

示例HTML:

<div data-ng-repeat="item in items | filter: filterOnlyFoo"></div>

样品Plunker

这篇关于AngularJs,过滤器不区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆