如何使用多个参数调用 Angular.js 过滤器? [英] How do I call an Angular.js filter with multiple arguments?

查看:25
本文介绍了如何使用多个参数调用 Angular.js 过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文档,我们可以调用过滤器,例如日期 像这样:

{{ myDateInScope |日期:'yyyy-MM-dd' }}

这里的日期是一个接受一个参数的过滤器.

从模板和 JavaScript 代码调用具有更多参数的过滤器的语法是什么?

解决方案

在模板中,您可以用冒号分隔过滤器参数.

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

在 Javascript 中,您将其称为

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

orderBy 过滤器文档中实际上隐藏了一个示例.><小时>

示例:

假设您制作了一个可以用正则表达式替换内容的过滤器:

myApp.filter("regexReplace", function() {//注册新过滤器return function(input, searchRegex, replaceRegex) {//过滤参数返回 input.replace(RegExp(searchRegex), replaceRegex);//执行};});

在模板中调用以删除所有数字:

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

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.

What is the syntax to call filters with more parameters both from templates and from JavaScript code?

解决方案

In templates, you can separate filter arguments by colons.

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

From Javascript, you call it as

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

There is actually an example hidden in the orderBy filter docs.


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>

这篇关于如何使用多个参数调用 Angular.js 过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆