Angular2-从变量分配管道 [英] Angular2 - assign pipe from a variable

查看:83
本文介绍了Angular2-从变量分配管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可能是这样的?

{{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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆