问题描述
我有一个过滤器,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.
我希望类似如下因素的工作,但$过滤器('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?
感谢您。
推荐答案
使用注入你的过滤器到 linkifyStuff
< FILTERNAME过滤器过滤
语法。像这样的:
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另一个过滤器调用一个过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!