本文介绍了如何使用角度JS过滤器格式的值在数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用角器做各种有趣的格式,但总是对的原始值。例如,数字滤波成货币格式。

I've used angular filters to do all kinds of fun formatting, but always on primitive values. For example, filtering numbers into a currency format.

我将如何做到这一点同类型的值的数组过滤的?例如:

How would I do that same kind of filtering for an array of values? For example

price = 1
prices = [1,2,3]

假设货币是

{{price | currency}}

将返回$ 1.00包装

would return $1.00

我想

{{prices | currency}}

返回[$ 1.00,$ 2.00,$ 3.00]

to return [$1.00,$2.00,$3.00]

我怎么能写一个过滤器,这是否?还是有不同的工具,我应该使用?

how can i write a filter that does that? Or is there a different tool i should be using?

有关更多的背景...我使用href=\"https://github.com/machineboy2045/angular-selectize\" rel=\"nofollow\">角selectize插件。我没有用在我的价值观NG-重复的选项。

For more background...I'm using this array inside of the angular-selectize plugin. I don't have the option of using ng-repeat on my values.

推荐答案

货币过滤器适用于单个值。如果你想应用到阵列的过滤器,你需要添加你自己的,如:

The currency filter applies to a single value. If you want a filter that applies to an array, you need to add your own, such as:

angular.module('yourModule')
.filter('currenyArray', function($filter) {
  return function(input, uppercase) {
    input = input || [];
    var out = [];
    for (var i = 0; i < input.length; i++) {
      out.push($filter('currency')(input[i]))
    }
    return out;
  };
})

然而,这会返回一个数组。如果你想要写的数组,你需要适应的函数来计算字符串而不是一个数组。

However this will return an array. If you do want to write the array, you need to adapt the function to compute a string instead of an array.

这篇关于如何使用角度JS过滤器格式的值在数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 03:06