过滤器在角度控制器内部不起作用

时间:2016-07-09 19:13:26

标签: javascript angularjs angularjs-filter

我想使用角度控制器内的过滤器对象来过滤对象列表。但它给了我过滤列表。它应该返回第二个对象。

这是我的代码示例

app.controller('MainCtrl', function($scope, $filter) {
  $scope.list=[
  {

    "job_id": 2,
    "description": "Bid for Job 2",
    "price": 115,
    "butler_id": 60,
    "butler_name": "Butler Client 1",
    "service_price": 500,
    "material_price": 900,
    "date_created": "23 Apr 1993"
  },
  {

    "job_id": 2,
    "description": "sfcs",
    "price": 555,
    "butler_id": 70,
    "butler_name": "Butler Client 2",
    "service_price": 666,
    "material_price": 666,
    "date_created": "23 Apr 1993"
  }
];

  $scope.filter ={"butler_name":"","service_price":"","material_price":"6","price":"","created_date":"","description":""};

 var getFiltered = $filter('filter')($scope.list, $scope.filter);
  $scope.filteredBids = getFiltered;

});

Plunker link

1 个答案:

答案 0 :(得分:0)

您正在传递过滤器中的所有属性,而Angular会考虑AND,因此您没有得到任何结果。

您必须根据过滤器创建属性。

 var filterParams = {};
        if ($scope.filter.butler_name.trim() !== "") {
            filterParams.butler_name = $scope.filter.butler_name;
        }
        if ($scope.filter.service_price.trim() !== "") {
            filterParams.service_price = $scope.filter.service_price;
        }
        if ($scope.filter.material_price.trim() !== "") {
            filterParams.material_price = $scope.filter.material_price;
        }
        if ($scope.filter.price.trim() !== "") {
            filterParams.price = $scope.filter.price;
        }
        if ($scope.filter.created_date.trim() !== "") {
            filterParams.created_date = $scope.filter.created_date;
        }
        if ($scope.filter.description.trim() !== "") {
            filterParams.description = $scope.filter.description;
        }
        var getFiltered = $filter('filter')($scope.list, filterParams);
        $scope.filteredBids = getFiltered;

Updated link