簡體   English   中英

使用轉換過濾器基於搜索輸入過濾ng-repeat

[英]Filter ng-repeat based on search input with transformation filter

我有一個記錄數組,我在帶有標題過濾器的HTML表上重復。 事實證明,某些值由過濾器轉換,從而使ng-repeat過濾器失敗。

<table class="table">
  <thead>
    <tr>
      <td><input ng-model="search.time" type="text" class="form-control" /></td>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="record in records | filter: search">
      <td>{{record.time | timeFormatter}}</td>
    </tr>
  </tbody>
</table>

如大家所見,表列中的值正在由timeFormatter過濾器轉換。 因此,記錄時間顯示為“ 08:00 AM”,而不是“ 0800”。 當用戶鍵入“ 08”時,它將起作用,但是如果用戶鍵入“ 08:”或“ AM”,它將不再起作用。

你們可以幫我使過濾器使用表格列中顯示的值(即格式化)嗎?

提前致謝。

您可能需要使用指令。 嘗試這個:

    angular.module("app").directive("myDirective", function(){
   return {
      require: 'ngModel',
      link: function(scope, element, attrs, ngModelController) {
        ngModelController.$parsers.push(function(data) {
          //convert data from view format to model format
           return input.substring(0, 2) + ':' + input.substring(2)
        });

        ngModelController.$formatters.push(function(data) {
          //convert data from model format to view format
         return input.substring(0, 2) + ':' + input.substring(2)
        });
      }
    };
});

這是一個笨蛋

這只是猜測,但是您的ng模型正在search.time上存儲更改,但是您的過濾器正在“過濾”搜索。 嘗試在過濾器中使用search.time。

暫無
暫無

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

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