问题描述
我有一个过滤器,linkifyStuff,我想在其中使用另一个过滤器处理一些变量.我无法弄清楚从另一个过滤器调用一个过滤器的语法.
I have a filter, linkifyStuff, in which I want some variables processed using another filter. I can't figure out the syntax to call one filter from another.
我知道过滤器链接——这不是我想要做的.我想将过滤器应用于 linkifyStuff 过滤器中的局部变量,而不是其输入或输出.
I know about filter chaining – that's not what I want to do. I want to apply a filter to a local variable in linkifyStuff filter, not to its input or output.
我希望像下面这样的东西可以工作,但 $filter('filtername') 显然不是正确的语法.
I would expect something like the folowing to work, but $filter('filtername') is not the correct syntax apparently.
module.filter('sanitizeStuff', function() {
// ...
})
module.filter('prettifyStuff', function() {
// ...
})
module.filter('linkifyStuff', function($filter) {
return function(text) {
// ...
// ...
return $filter('sanitizeStuff')(foo) + ' whatever ' + $filter('prettifyStuff')(bar)
}
})
我可以为 sanitizeStuff 和 sanitizeStuff 编写一个普通的 js 函数,并从这些过滤器中调用该函数,但这似乎是错误的.关于如何以角度方式进行操作的任何建议?
I could write a plain js functions for sanitizeStuff and sanitizeStuff and call that function from these filters but this seems wrong. Any advice on how to do it the angular way?
谢谢.
推荐答案
使用 Filter
语法将过滤器注入 linkifyStuff
.像这样:
Inject your filters into linkifyStuff
using <filterName>Filter
syntax. Like this:
app.filter('linkifyStuff', function(sanitizeStuffFilter,prettifyStuffFilter) {
return function(text) {
return sanitizeStuffFilter(text) + ' whatever ' + prettifyStuffFilter(text);
}
});
这篇关于如何在 angular.js 中从另一个过滤器调用一个过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!