角度过滤器表由控制器内的文本组成

时间:2016-08-18 11:37:34

标签: angularjs angular-filters

我想在filter内创建controller,其操作方式如下:

<tr ng-repeat = "obj in filterd = (allObjs | filter:{field: fieldFilter} | filter {field2: filed2Filter})"> //more filters...

所以它会在allObjsfieldFilter内对field2Filter数组进行过滤,在我的控制器中,我不明白如何使用filter文档来理解它:< / p>

$scope.filterTable = $filter('filter')(fieldFilter)(field2ilter); //doesn't work

感谢。

2 个答案:

答案 0 :(得分:2)

$filter可以拍摄一个物体。

  

例如{name:&#34; M&#34;,phone:&#34; 1&#34;}谓词将返回一个数组   属性名称包含&#34; M&#34;和财产电话   包含&#34; 1&#34;。

在控制器中使用$ filter的示例:

&#13;
&#13;
angular.module('filters', [])
.controller('demo', function($scope, $filter) {
  $scope.example1 = [{
    name: 'C#',
    type: 'static'
  }, {
    name: 'PHP',
    type: 'dynamic'
  }, {
    name: 'Go',
    type: 'static'
  }, {
    name: 'JavaScript',
    type: 'dynamic'
  }, {
    name: 'Rust',
    type: 'static'
  }];

  $scope.filteredLanguages = $filter('filter')($scope.example1, {
    name: 'C#',
    type: 'static'
  });
})
&#13;
<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
</head>

<body>
  <div class="container">
    <div class="row">
      <div class="col-lg-6 col-lg-offset-3">
        <div ng-app="filters">
          <div ng-controller="demo">
            <div class="panel panel-default">
              <div class="panel-body">
                <h4 class="text-center">AngularJS Filter</h4>
                <p><strong>Original:</strong>
                </p>
                <ul class="list">
                  <li ng-repeat="lang in example1">{{lang.name}}</li>
                </ul>
                <p><strong>Languages Filtered:</strong>
                </p>
                <ul class="list">
                  <li ng-repeat="lang in filteredLanguages">{{lang.name}}</li>
                </ul>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>

</html>
&#13;
&#13;
&#13;

此外,过滤器只返回一个数组,因此您可以使用每个属性键调用过滤器两次,而不是上面的过滤器,请参阅此answer

var filtered;
filtered = $filter('filter')($scope.list, {name: $scope.filterParams.nameSearch});
filtered = $filter('orderBy')(filtered, $scope.filterParams.order);

答案 1 :(得分:1)

类似于在标记中级联过滤器的方法,只需将它们级联在控制器中:

$scope.filterTable = $filter('filter')(
    $filter('filter')(allObjs, fieldFilter) //results of first filter is source for 2nd one
    , field2Filter);