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

从文档开始,我们可以像这样调用过滤器,例如date:


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

date是一个采用一个参数的过滤器。


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


精慕HU
浏览 699回答 3
3回答

慕雪6442864

在模板中,可以用冒号分隔过滤器参数。{{ yourExpression | yourFilter: arg1:arg2:... }}在Javascript中,您将其称为$filter('yourFilter')(yourExpression, arg1, arg2, ...)实际上,orderBy筛选器文档中隐藏了一个示例。例:假设您制作了一个可以用正则表达式替换内容的过滤器:myApp.filter("regexReplace", function() { // register new filter&nbsp; return function(input, searchRegex, replaceRegex) { // filter arguments&nbsp; &nbsp; return input.replace(RegExp(searchRegex), replaceRegex); // implementation&nbsp; };});调用模板以检查所有数字:<p>{{ myText | regexReplace: '[0-9]':'X' }}</p>

慕桂英4014372

我在下面提到的地方也提到了自定义过滤器,如何调用这些具有两个参数的过滤器countryApp.filter('reverse', function() {&nbsp; &nbsp; return function(input, uppercase) {&nbsp; &nbsp; &nbsp; &nbsp; var out = '';&nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0; i < input.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out = input.charAt(i) + out;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (uppercase) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out = out.toUpperCase();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return out;&nbsp; &nbsp; }});并使用模板从html中调用该过滤器,如下所示<h1>{{inputString| reverse:true }}</h1>如果看到的话,第一个参数是inputString,第二个参数是true,它使用:符号与“ reverse”组合
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

AngularJS