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

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

问题描述

文档,我们可以调用一个过滤器等的date 是这样的:

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.

什么是调用既从模板和从JavaScript code?

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:... }}

从Javascript,你怎么称呼它为

From Javascript, you call it as

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

实际上有隐藏在排序依据过滤文档的例子。

示例:

假设你做一个过滤器,可与常规的前pressions替换件事情:

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天全站免登陆