本文翻译自: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'});