使用上的NG-选择过滤器以更改显示的值 [英] Use filter on ng-options to change the value displayed
问题描述
我有价格(数组 0
, 0.99
, 1.99
...等),我想在显示<选择方式>
I have an array of prices (0
, 0.99
, 1.99
... etc) that I want to display in <select>
.
我想用角的 NG-选项
像这样
<select ng-model="create_price" ng-options="obj for obj in prices"/>
由于它是显示它上面将产生一系列的 0
, 0.99
, 1.99
...
但我想在code使用过滤器,这样每次字的'价格'的是presented(或类似的东西),在code会运行一个函数的浮点数更改为字符串和present(免费
, 0.99 $
, 1.99 $
...等)。
But I want to use a filter in the code such that every time the word 'prices' is presented (or something like that), the code will run a function to change the float numbers to strings and present (free
, 0.99$
, 1.99$
... etc).
我有没有办法做到这一点?
I there a way to do that?
感谢
推荐答案
有一个更好的方法:
app.filter('price', function() {
return function(num) {
return num === 0 ? 'free' : num + '$';
};
});
然后使用它是这样的:
Then use it like this:
<select ng-model="create_price" ng-options="obj as (obj | price) for obj in prices">
</select>
此方式中,过滤器是用于单值是有用的,而不是只在阵列操作。如果你有对象和相应的格式过滤器,这是非常有用的。
This way, the filter is useful for single values, rather than operating only on arrays. If you have objects and corresponding formatting filters, this is quite useful.
过滤器也可直接在code使用,如果需要的话:
Filters can also be used directly in code, if you need them:
var formattedPrice = $filter('price')(num);
这篇关于使用上的NG-选择过滤器以更改显示的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!