Angular2-从变量分配管道 [英] Angular2 - assign pipe from a variable
问题描述
是否可能是这样的?
{{property | some_variable_name}}
我的目标是使用在JSON
配置(或变量)中设置的pipe
,但是我不确定是否可以通过这种方式在插值中传递pipe
名称.
有人以前尝试过类似的东西吗?
My goal is to use a pipe
that is set in a JSON
configuration (or a variable), but I'm not sure if it is possible to pass the pipe
name in the interpolation this way.
Anybody tried something like this before?
推荐答案
该管道需要在@Component()
装饰器或PLATFORM_PIPES
中的pipes: [MyPipeHere]
中列出,因此该方法无效.
The pipe needs to be listed in pipes: [MyPipeHere]
in the @Component()
decorator or in PLATFORM_PIPES
, therefore this approach won't work.
您可以做的是创建一个调用其他管道"的自定义管道.管道基本上只是一个功能,因此这很容易.
What you can do instead is creating a custom pipe that calls other "pipes". A pipe is basically just a function, therefore this is easy.
您可以像这样将变量传递到自定义管道
You can pass a variable to your custom pipe like
{{property | my_custom_pipe:some_variable_name}}
这应该非常接近您想要的.
this should be pretty close to what you want.
这篇关于Angular2-从变量分配管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!