我试图通过使用我的过滤器声明中的方法返回object literal
来创建参数化过滤器。也就是说,我希望能够{{ input | myFilter.foo | myFilter.bar }}
等等...换句话说,我正在尝试命名空间我的过滤器。
我希望它会起作用,但我会收到如下错误:
未知提供者:myFilter.fooFilterProvider< - myFilter.fooFilter
我知道我可以将信息传递给像{{ input | filter:foo }}
这样的过滤器,但据我所知,只能使用字符串(?)而且我必须切换这个值,而不是仅仅能够使用方法直接返回对象文字。
angular.module('plunker', [])
.filter('greet', function() {
var greetFactory = function(prefix) {
return function(name) {
return prefix + name + '!'
}
}
return {
hello: greetFactory('Hello'),
wassup: greetFactory('Wassup'),
yo: greetFactory('Yo!')
}
});
<p>{{"Zach" | greet.hello}} // should output: Hello Zach!</p>
<p>{{"Bill" | greet.yo}} // should output: Yo Bill!</p>
<p>{{"Bob" | greet.wassup}} // should output: Wassup Bob!</p>
答案 0 :(得分:1)
是的,您可以在过滤器中添加其他过滤器作为依赖项
app.filter('myFilter', ['greetFilter', function(greetFilter){
}])
请注意,您必须添加&#34;过滤&#34;在您要注入的过滤器名称的末尾。然后,您可以使用这些过滤器来构建自己的过滤器。