Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 views
in Technique[技术] by (71.8m points)

javascript - Angular2 - assign pipe from a variable

Is it possible something like this:

{{property | some_variable_name}}

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...