为什么$ filter没有显示过滤数据?

时间:2017-06-20 07:42:47

标签: javascript angularjs angularjs-directive

我正在尝试使用$ filter .My输入参数

来过滤我的列表
"TO" : "DEL"
"FROM" :"PNQ"
"DEPART Date" : "10-01-2017"

它应该显示一个输出。这个

{
    "to_station_name": "Delhi",
    "to_station": "DEL",
    "from_station": "PNQ",
    "from_station_name": "Pune",
    "depart_time": "12:00AM",
    "arrival_time": "4:00PM",
    "PNR": "AL_201",
    "flight_date": "10-01-2017",
    "fare": "900"
  },

这是我的代码 https://plnkr.co/edit/k4FBxqufETslgYxm4zEx?p=preview

目前,当我点击search按钮

时,它没有显示任何内容
$scope.searchClick =function(){
  if($scope.fromStation!='' && $scope.toStation!='' && $scope.departDate !=''){

     $scope.names = $filter('filter')($scope.defaultNames, function(item){
        var time1 = moment(item.flight_date, "DD-MM-YYYY");
       var time2 = moment($scope.departDate, "DD-MM-YYYY");
       return item.from_station_name ===  $scope.fromStation &&
       item.to_station_name === $scope.toStation && 
       time1.valueOf() == time2.valueOf()

     });

  }


}

3 个答案:

答案 0 :(得分:0)

重构您的$scope.searchClick()

$scope.searchClick = function() {
  if ($scope.fromStation !== '' && $scope.toStation !== '' && $scope.departDate !== '') {
    $scope.names = $filter('filter')($scope.defaultNames, function(item) {
      var time1 = moment(item.flight_date, "DD-MM-YYYY");
      var time2 = moment($scope.departDate, "DD-MM-YYYY");
      var times = time1.valueOf() == time2.valueOf();
      $scope.fromStation = item.from_station_name;
      $scope.toStation = item.to_station_name;
      return $scope.fromStation, $scope.toStation, times;
    });
  }
};

这是return声明的问题。

plnkr

答案 1 :(得分:0)

请检查更新的plunker。等于条件是错误的。我修改了如下的return语句

if(item.from_station ===  $scope.fromStation &&
       item.to_station === $scope.toStation && time1.valueOf() == time2.valueOf() ){
         return item
       }

https://plnkr.co/edit/lIh1pGs2tmp3hWhTPMcX?p=preview

答案 2 :(得分:0)

更改您的SearchClick功能,如下所示

    $scope.searchClick =function(){
  if($scope.fromStation!='' && $scope.toStation!='' && $scope.departDate !=''){

     $scope.names = $filter('filter')($scope.defaultNames, function(item){
        var time1 = moment(item.flight_date, "DD-MM-YYYY");
       var time2 = moment($scope.departDate, "DD-MM-YYYY");
       return item.from_station ===  $scope.fromStation &&
       item.to_station === $scope.toStation && 
       time1.valueOf() === time2.valueOf()

     });

  }


}

输入你输入错误,$ scope.departDate是MM / DD / YYYY格式,将输入更改为
"至" :" DEL" " FROM" :" PNQ" " DEPART日期" :" 01-10-2017" // 2017年1月10日