Angularjs 使用“ng上的过滤器”选项更改显示的值

Angularjs 使用“ng上的过滤器”选项更改显示的值,angularjs,filter,ng-options,Angularjs,Filter,Ng Options,我有一个价格数组(0,0.99,1.99…等等),我想在中显示 我想像这样使用Angular的ng选项 <select ng-model="create_price" ng-options="obj for obj in prices"/> 当它显示在上面时,它将生成一个选择0,0.99,1.99 但是我想在代码中使用一个过滤器,这样每当出现“prices”这个词(或类似的词)时,代码都会运行一个函数,将浮点数更改为字符串并显示(free,0.99$,1.99$…等等) 我有办

我有一个价格数组(
0
0.99
1.99
…等等),我想在
中显示

我想像这样使用Angular的
ng选项

<select ng-model="create_price" ng-options="obj for obj in prices"/>

当它显示在上面时,它将生成一个选择
0
0.99
1.99

但是我想在代码中使用一个过滤器,这样每当出现“prices”这个词(或类似的词)时,代码都会运行一个函数,将浮点数更改为字符串并显示(
free
0.99$
1.99$
…等等)

我有办法吗


谢谢

您想创建自定义筛选器,例如:

app.filter('price', function() {
  return function(arr) {
    return arr.map(function(num){
      return num === 0 ? 'free' : num + '$';
    });
  };
});
像这样使用它:

<select ng-model="create_price" ng-options="obj for obj in prices | price">
  {{ obj }}
</select>

{{obj}}
有一个更好的方法:

app.filter('price', function() {
  return function(num) {
    return num === 0 ? 'free' : num + '$';
  };
});
然后像这样使用它:

<select ng-model="create_price" ng-options="obj as (obj | price) for obj in prices">
</select>
请原谅伪代码

data-ng-options="( obj.property | myFilter ) for obj in objects"

app.filter('myFilter', function() {
  return function(displayValue) {
    return (should update displayValue) ? 'newDisplayValue' : displayValue;
});

谢谢除了您添加的语法错误-不需要第四行圆括号。除此之外,答案非常有效!为清楚起见,您可以在表达式中添加括号:
obj as(obj | price)For obj in prices
。语法没有那么吓人。如果有其他人到这里来搜索用于
ng options
的标准货币过滤器,那么这可能很有用:
option as(option | currency:'$:0)对于pricesArr中的option
请尝试进一步充实您的答案。仅仅发布一个代码块而没有任何解释或上下文是不好的。在当前状态下,您的答案可能对将来寻找此问题答案的人没有多大用处。谢谢!这就是我要找的<代码>(obj.property | myFilter)用于对象中的obj
,但是的,解释是关键
data-ng-options="( obj.property | myFilter ) for obj in objects"

app.filter('myFilter', function() {
  return function(displayValue) {
    return (should update displayValue) ? 'newDisplayValue' : displayValue;
});