Core: How to use the Pipe outside of the template ?

Created on 15 Nov 2016  路  6Comments  路  Source: ngx-translate/core

Hello,

I thought the Pipe would be injectable in the constructor of my component so I can call translatePipe.transform(mytext)...

What is the way to do this ?

thank you

questiosupport

Most helpful comment

You can use "instant": this.translateService.instant('countryOfOriginCode'). Be careful, like the pipe transform, this is synchronous so you have to make sure that the translations have been loaded, otherwise it will return the key instead of the translation

All 6 comments

Hello,

yes you can do that, see the tests: https://github.com/ocombe/ng2-translate/blob/master/tests/translate.pipe.spec.ts#L87
But then at the point you might as well use the service which does the same with a lot less of overhead :)

Hello and thank you,

the problem not in test, is that the pipe is not available as a provider, so I can't inject it.

How am I supposed to get the pipe ?

thank you

I guess that you have to provide it either in your module or in your component (using the providers property), but like I said you really should be using the service instead.

ok I follow your advice,
thank you

@ocombe
Using as pipe is shorter than making sub. Very useful when creating dynamics forms.
this.translatePipe.transform('countryOfOriginCode'),
Please add this metod to docs :+1: or please show me better way.

You can use "instant": this.translateService.instant('countryOfOriginCode'). Be careful, like the pipe transform, this is synchronous so you have to make sure that the translations have been loaded, otherwise it will return the key instead of the translation

Was this page helpful?
0 / 5 - 0 ratings

Related issues

crebuh picture crebuh  路  3Comments

egornoveo picture egornoveo  路  4Comments

chris31389 picture chris31389  路  3Comments

dankerk picture dankerk  路  3Comments

IterationCorp picture IterationCorp  路  3Comments