如何使用多个参数调用Angular.js过滤器?

本文翻译自:How do I call an Angular.js filter with multiple arguments?

As from the documentation , we can call a filter such as date like this: 文档中我们可以调用像这样的日期过滤器:

{{ myDateInScope | date: 'yyyy-MM-dd' }}

Here date is a filter that takes one argument. 这里date是一个带有一个参数的过滤器。

What is the syntax to call filters with more parameters both from templates and from JavaScript code? 从模板和JavaScript代码调用带有更多参数的过滤器的语法是什么?


#1楼

参考:https://stackoom.com/question/165T3/如何使用多个参数调用Angular-js过滤器


#2楼

In templates, you can separate filter arguments by colons . 在模板中,您可以使用冒号分隔过滤器参数。

{{ yourExpression | yourFilter: arg1:arg2:... }}

From Javascript, you call it as 从Javascript,你称之为

$filter('yourFilter')(yourExpression, arg1, arg2, ...)

There is actually an example hidden in the orderBy filter docs. 实际上, orderBy过滤器文档中隐藏了一个示例。


Example: 例:

Let's say you make a filter that can replace things with regular expressions: 假设您创建了一个可以用正则表达式替换事物的过滤器:

myApp.filter("regexReplace", function() { // register new filter

  return function(input, searchRegex, replaceRegex) { // filter arguments

    return input.replace(RegExp(searchRegex), replaceRegex); // implementation

  };
});

Invocation in a template to censor out all digits: 在模板中调用以检查所有数字:

<p>{{ myText | regexReplace: '[0-9]':'X' }}</p>

#3楼

i mentioned in the below where i have mentioned the custom filter also , how to call these filter which is having two parameters 我在下面提到过我自己也提到了自定义过滤器,如何调用这些有两个参数的过滤器

countryApp.filter('reverse', function() {
    return function(input, uppercase) {
        var out = '';
        for (var i = 0; i < input.length; i++) {
            out = input.charAt(i) + out;
        }
        if (uppercase) {
            out = out.toUpperCase();
        }
        return out;
    }
});

and from the html using the template we can call that filter like below 从使用模板的html我们可以像下面那样调用该过滤器

<h1>{{inputString| reverse:true }}</h1>

here if you see , the first parameter is inputString and second parameter is true which is combined with "reverse' using the : symbol 在这里,如果你看到,第一个参数是inputString,第二个参数是true,它与使用:符号的“反向”组合


#4楼

If you want to call your filter inside ng-options the code will be as follows: 如果要在ng-options中调用过滤器,代码如下:

ng-options="productSize as ( productSize | sizeWithPrice: product )  for productSize in productSizes track by productSize.id"

where the filter is sizeWithPriceFilter and it has two parameters product and productSize 过滤器的大小是sizeWithPriceFilter,它有两个参数product和productSize


#5楼

If you need two or more dealings with the filter, is possible to chain them: 如果您需要与过滤器进行两次或更多次交易,可以将它们链接起来:

{{ value | decimalRound: 2 | currencySimbol: 'U$' }} 
// 11.1111 becomes U$ 11.11

#6楼

像这样:

var items = $filter('filter')(array, {Column1:false,Column2:'Pending'});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值