【发布时间】:2015-05-15 13:29:21
【问题描述】:
我想按属性过滤对象列表并使用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>
【问题讨论】:
-
可能是
{{ formattedDuration = (video.duration | formatDate:'HH:mm:ss') }}会起作用。
标签: javascript angularjs angularjs-ng-repeat angularjs-filter