AngularJS学习笔记

8.2. 过滤列表 filter

filter 是一个过滤内容的标签。

如果参数是一个字符串,则列表成员中的任意属性值中有这个字符串,即为满足条件(忽略大小写):

  <div ng-controller="TestCtrl">
    {{ data | filter: 'b' }} <br />
    {{ data | filter: '!B' }} <br />
  </div>


  <script type="text/javascript">
  var TestCtrl = function($scope){
    $scope.data = [
      {name: 'B', age: 4},
{name: 'A', age: 1},
{name: 'D', age: 3},
{name: 'C', age: 3},
]; } angular.bootstrap(document.documentElement); </script>

可以使用对象,来指定属性名, $ 表示任意属性:

  {{ data | filter: {name: 'A'} }} <br />
  {{ data | filter: {$: '3'} }} <br />
  {{ data | filter: {$: '!3'} }} <br />

自定义的过滤函数也支持:

  <div ng-controller="TestCtrl">
    {{ data | filter: f }} <br />
  </div>


  <script type="text/javascript">
  var TestCtrl = function($scope){
    $scope.data = [
      {name: 'B', age: 4},
{name: 'A', age: 1},
{name: 'D', age: 3},
{name: 'C', age: 3},
]; $scope.f = function(e){ return e.age > 2; } } angular.bootstrap(document.documentElement); </script>