【问题标题】:filter with multiple values of same properties of an object使用对象的相同属性的多个值进行过滤
【发布时间】:2015-12-30 18:26:33
【问题描述】:

我有一个名为tasks 的对象。它具有各种特性。现在我只想显示状态为to-doin-progress 的任务。请帮助我如何在ng-repeat 中应用过滤器。

$scope.tasks = [
 {
   'title': 'task one',
   'status': 'to-do'
 },

 {
   'title': 'task two',
   'status': 'in-progress'
 },

 {
   'title': 'task three',
   'status': 'completed'
 }

]

<div ng-repeat="task in tasks">
    <h1>{{task.name}}</h1>
    <p>{{task.status}}</p>
</div>

【问题讨论】:

    标签: javascript angularjs angularjs-ng-repeat angular-filters


    【解决方案1】:
    <div ng-repeat="task in tasks | filter: filteredTasks">
        <h1>{{task.name}}</h1>
        <p>{{task.status}}</p>
    </div>
    

    创建过滤器:

    $scope.filteredTasks = function(task) {
      return (task.status === 'to-do' || task.status === 'in-progress') ? true: false;
    });
    

    【讨论】:

    • 感谢@Avijit。这个对我有用。其实我在想不同的方式。但这是更好的方法。快乐编码:-)
    【解决方案2】:

    您需要为过滤器指定一个自定义函数。 这是一个有效的plnkr

    代码:

    <div ng-repeat="task in tasks | filter:status">
        <h1>{{task.name}}</h1>
        <p>{{task.status}}</p>
    </div>
    

    js:

    $scope.status = function(item) {
      if (item.status == 'to-do' || item.status == 'in-progress') return true;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 1970-01-01
      • 1970-01-01
      • 2012-12-16
      • 1970-01-01
      • 2014-03-28
      • 1970-01-01
      相关资源
      最近更新 更多