Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS过滤器(函数和输入搜索一起)_Angularjs - Fatal编程技术网

AngularJS过滤器(函数和输入搜索一起)

AngularJS过滤器(函数和输入搜索一起),angularjs,Angularjs,我想通过使用AngularJS同时使用过滤函数和输入搜索。我可以处理多个功能。但是我在为搜索添加文本框时出现了一些错误。有没有办法做到这一点?我尝试了一些方法,但没有人不起作用 提前谢谢 下面是示例代码 var-app=angular.module(“app”,[]), url=”http://www.filltext.com/?rows=50&pretty=true&fName={firstName}&年龄=[18,19,20,21]”; app.controller(“controller

我想通过使用AngularJS同时使用过滤函数和输入搜索。我可以处理多个功能。但是我在为搜索添加文本框时出现了一些错误。有没有办法做到这一点?我尝试了一些方法,但没有人不起作用

提前谢谢

下面是示例代码

var-app=angular.module(“app”,[]),
url=”http://www.filltext.com/?rows=50&pretty=true&fName={firstName}&年龄=[18,19,20,21]”;
app.controller(“controller”,函数($scope,$http){
$http.get(url)
.成功(职能(resp){
$scope.list=resp;
});
$scope.filterAge=函数(项){
返回项目。年龄>19岁;
};
});

AngularJS过滤器

名称 年龄 {{item.fName} {{item.age}

观察者正在等待对搜索字段采取任何操作,然后执行该函数

您应该向角度模块添加自定义过滤器,而不是向控制器添加过滤器功能。例如:

自定义过滤器:

.filter('filterByAge', function filterByAge() {
  return function(array, age) {
    var filteredArray = [];

    angular.forEach(array, function(item) {
      if (item.age > age) {
        filteredArray.push(item);
      }
    });

    return filteredArray;
  }
});
HTML

<input type="text" ng-model="search" placeholder="Search..." />
<table class="table table-striped">
  <thead>
    <th>Name</th>
    <th>Age</th>
  </thead>
  <tbody>
    <tr ng-repeat="item in list | filter: search | filterByAge:19">
      <td>{{ item.fName }}</td>
      <td>{{ item.age }}</td>
    </tr>
  </tbody>
</table>

名称
年龄
{{item.fName}
{{item.age}
<input type="text" ng-model="search" placeholder="Search..." />
<table class="table table-striped">
  <thead>
    <th>Name</th>
    <th>Age</th>
  </thead>
  <tbody>
    <tr ng-repeat="item in list | filter: search | filterByAge:19">
      <td>{{ item.fName }}</td>
      <td>{{ item.age }}</td>
    </tr>
  </tbody>
</table>