簡體   English   中英

角日期范圍過濾器不起作用

[英]Angular Date Range Filter Not Working

我試圖弄清楚為什么我的日期范圍過濾器不起作用,並且引發錯誤-[$ injector:unpr]未知提供程序:dateRangeProvider <-dateRange <-dashboardController。 我嘗試將'dateRange'放入我的依賴項和$ filter中,不確定我所做的是否正確。 任何幫助都非常感謝! 謝謝!

我正在嘗試在兩個日期之間進行過濾以獲取產品ID的HTML

<input style="width:200px; padding:10px; border-radius:4px;"type="text" placeholder="search name" ng-model='productFilter'>
<input type="date" ng-model="to_date">
<input type="date" ng-model="from_date">
<div ng-repeat="product in products | dateRange : from_date : to_date | filter: productFilter track by $index">
  <p>Total Inputs: {{product.createdAt}}{{product._id}}</p>
</div>

這是我的DashboardController

app.controller('dashboardController', ['$scope', '$filter', 'OpFactory', function($scope, $filter, OpFactory){

function getProducts(){
  OpFactory.getProducts(function(data){
   $scope.products = data;
   console.log("data from getProducts function in dashboardcontroller", data);
 })
}
getProducts();

$filter('dateRange', function() {
       return function( product, fromDate, toDate ) {
           var filtered = [];
           console.log(fromDate, toDate);
           var from_date = Date.parse(fromDate);
           var to_date = Date.parse(toDate);
           angular.forEach(product, function(item) {
               if(item.completed_date > from_date && product.completed_date < to_date) {
                   filtered.push(item);
               }
           });
           return filtered;
       };
   });
}]);

有幾個問題,但主要問題是如何創建過濾器。 過濾器應該附加到模塊中,例如angular.module('mymodule', []).filter('dateRange', fn)

除此之外,您還必須處理過濾器尚未完全填充時要呈現的內容,並且過濾器函數中有product.completed_date而不是item.completed日期。

這里有一個工作示例(只是缺少輸入過濾器尚未完全填充時的處理方式):

 angular.module('webapp', []) .filter('dateRange', function () { return function(product, fromDate, toDate) { var filtered = []; console.log(fromDate, toDate); if (!fromDate && !toDate) { return product; } var from_date = Date.parse(fromDate); var to_date = Date.parse(toDate); angular.forEach(product, function(item) { if (item.createdAt > from_date && item.createdAt < to_date) { filtered.push(item); } }); return filtered; }; }) .controller('dashboardController', ['$scope', '$filter', '$timeout', function($scope, $filter, $timeout) { $scope.products = []; function getProducts() { $timeout(function() { $scope.products = [{ _id: 1, createdAt: new Date(2000, 1, 1) }, { _id: 2, createdAt: new Date(2001, 1, 1) }, { _id: 3, createdAt: new Date(2002, 1, 1), }, { _id: 4, createdAt: new Date(2003, 1, 1) }]; }, 500); } getProducts(); }]); 
 <!DOCTYPE html> <html ng-app="webapp"> <head> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.9/angular.min.js"></script> </head> <body ng-app="webapp"> <div ng-controller="dashboardController"> <input style="width:200px; padding:10px; border-radius:4px;"type="text" placeholder="search name" ng-model='productFilter'> <input type="date" ng-model="from_date"> <input type="date" ng-model="to_date"> <div ng-repeat="product in products | dateRange : from_date : to_date | filter: productFilter track by $index"> <p>Total Inputs: {{product.createdAt}} :: {{product._id}}</p> </div> </div> </body> </html> 

對於其他有興趣的人,這是我想出的答案。

app.filter('dateRange', function () {
 return function(product, fromDate, toDate) {
     var filtered = [];
     console.log(fromDate, toDate, filtered);

     if (!fromDate && !toDate) {
         return product;
     }

     var from_date = Date.parse(fromDate);
     var to_date = Date.parse(toDate);
     angular.forEach(product, function(item) {
       console.log('in the forEach function')
       var createdAt = Date.parse(item.createdAt);
         if (from_date && to_date && createdAt > from_date && createdAt < to_date) {
           console.log('in the if statement', filtered)
           filtered.push(item);
         }
     })

    return filtered;

 };
})

我的HTML表格

<table class="receivingDataTable" datatable='ng' style="width: 1000px;">
 <tr>
    <th>Date</th>
    <th>Container #</th>
    <th>Cut #</th>
    <th>CTNS #</th>
    <th>Pieces</th>
    <th>Receiving #</th>
    <th>Remarks</th>
  </tr>
  <tr ng-repeat="product in filtered = (products | dateRange: from_date : to_date)">
    <td>{{product.createdAt | date: "MM/dd/yyyy"}}</td>
    <td>{{product.container_number}}</td>
    <td>{{product.cut_number}}</td>
    <td>{{product.ctns_number}}</td>
    <td>{{product.pieces}}</td>
    <td>{{product._id}}</td>
    <td>{{product.remarks}}</td>
  </tr>
</table>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM