javascript - AngularJS - 按应用了过滤器的对象属性过滤列表

标签 javascript angularjs angularjs-ng-repeat angularjs-filter

我想按对象的属性过滤对象列表,并使用了 filter 过滤器。问题在于某些对象属性应用了过滤器,这些过滤器会更改它们的显示格式(请参阅下面代码中的 formatDate 过滤器),并且 filter 过滤器将使用未格式化的对象属性。

例如:有一个 .duration = 150(秒)的视频将显示为 00:02:30(由于 formatDate 过滤器)。如果用户搜索“02”,将找不到视频,因为搜索了视频的 .duration 属性,而“02”与 150 不匹配。

按显示值而不是“原始”值进行过滤的最简单方法是什么?
我考虑过为列表中的每个对象添加一个 getDurationFormatted() 函数,并根据该属性专门显示和过滤,但这会严重降低 HTML 的表现力。

<input ng-model="query">

<tr ng-repeat="video in videos | filter:query">
    <td>
        {{ video.name }}
    </td>
    <td>
        <!-- The formatDate filter is used to format the amount of seconds -->
        {{ video.duration | formatDate:'HH:mm:ss' }}
    </td>
</td>

最佳答案

您可以使用与用户将看到的内容相对应的额外预格式化属性来扩展数组中的每个对象。然后过滤将用于用户友好的输入。

这是最简单的解决方案,只需要对模板进行少量修改:

<tr ng-repeat="video in videos | filter:query">
    <td>
        {{ video.name }}
    </td>
    <td>
        <!-- The formatDate filter is used to format the amount of seconds -->
        {{ video.durationFormatted = (video.duration | formatDate:'HH:mm:ss') }}
    </td>
</tr>

演示: http://plnkr.co/edit/7JxdCAJtPamMWsfVSwaN?p=preview

关于javascript - AngularJS - 按应用了过滤器的对象属性过滤列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30260778/

相关文章:

javascript - 通过 ng-repeat 循环中其他数组的包含来过滤数组

javascript - AngularJS - 范围在链接函数中重置,我做错了什么?

javascript - Angularjs 与 Internet Explorer 11,安全问题

javascript - 测试 JSONP $resource AngularJS

javascript - 使用 jquery 打开对话框

angularjs - postman 不就像浏览器一样吗?

javascript - Ionic-AngularJS : Emitting Event To Parent Only ReBroadcasts To Child That Emitted Event

javascript - AngularJS - 用 $q promise 的函数在测试中没有解析

javascript - Golang 后端到 javascript JSON Parse

javascript - AngularJS 将 ui-view 嵌套到 ng-repeat 中