Angularjs 为什么我的排序过滤器对所有东西都有效,除了“字母”;m?“;

Angularjs 为什么我的排序过滤器对所有东西都有效,除了“字母”;m?“;,angularjs,angular-filters,ng-filter,Angularjs,Angular Filters,Ng Filter,我在AngularJS中创建了一个小的过滤器应用程序,它可以工作,这样当我输入一封信时,它可以找到表中包含该信的所有内容。例如,如果我键入一个“s”,那么我会得到所有带有“s”的名称。为什么有些字母,比如字母“m”根本不起作用?有人能告诉我如何改变这一点,使所有的信件工作?非常感谢 HTML <!DOCTYPE html> <html lang="en"> <head> <title>The Sort Filter</title&

我在AngularJS中创建了一个小的过滤器应用程序,它可以工作,这样当我输入一封信时,它可以找到表中包含该信的所有内容。例如,如果我键入一个“s”,那么我会得到所有带有“s”的名称。为什么有些字母,比如字母“m”根本不起作用?有人能告诉我如何改变这一点,使所有的信件工作?非常感谢

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>The Sort Filter</title>
    <link href="content/css/styles.css" rel="stylesheet" type="text/css" />
    <script src="app\lib\angular.min.js"></script>
    <script src="app\app.js"></script>

  </head>
  <body ng-app="myApp">
    <div ng-controller="myController">
        <input type="text" ng-model="search" />
        <table>
            <thead>
                <tr>
                    <th>Firstname</th>
                    <th>Lastname</th>
                    <th>Gender</th>
                    <th>Salary</th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="employee in employees | orderBy: 'lastName' | filter: search"{{employee.lastName}}">
                    <td>{{ employee.firstName}}</td>
                    <td>{{ employee.lastName}}</td>
                    <td>{{ employee.gender}}</td>
                    <td>{{ employee.salary}}</td>
                </tr>
            </tbody>
        </table>
    </div>
  </body>
</html>
它实际上在工作。“m”返回所有数据,因为“男性”和“女性”。如果只想按名字和姓氏过滤,则必须创建自己的自定义过滤器。例如:

他认为:

<tr ng-repeat="employee in employees | orderBy: 'lastName' | myFilter: search">

和过滤器:

filter('myFilter', function() {
  return function(array, search) {      
    if (search !== undefined && search.length > 0) {        
      var new_array = [];        
      for (var i = 0; i < array.length; i++) {
        if (array[i].firstName.toLowerCase().indexOf(search.toLowerCase()) > -1 
         || array[i].lastName.toLowerCase().indexOf(search.toLowerCase()) > -1)
          new_array.push(array[i]);
      }        
      return new_array;
    } else {
      return array;
    }
}});
filter('myFilter',function(){
返回函数(数组,搜索){
如果(search!==未定义&&search.length>0){
var new_数组=[];
对于(var i=0;i-1
||数组[i].lastName.toLowerCase().indexOf(search.toLowerCase())>-1)
新的_array.push(array[i]);
}        
返回新的_数组;
}否则{
返回数组;
}
}});

您想对集合的特定属性或单个属性(如
firstName
/
lastName
)进行筛选?制作JSIDLE:)我希望能够键入firstName或lastName的字母并获得结果。哈,“男性/女性”-我应该想到这一点。非常感谢。
filter('myFilter', function() {
  return function(array, search) {      
    if (search !== undefined && search.length > 0) {        
      var new_array = [];        
      for (var i = 0; i < array.length; i++) {
        if (array[i].firstName.toLowerCase().indexOf(search.toLowerCase()) > -1 
         || array[i].lastName.toLowerCase().indexOf(search.toLowerCase()) > -1)
          new_array.push(array[i]);
      }        
      return new_array;
    } else {
      return array;
    }
}});