Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-evaluation of translate pipes if ChangeDetectionStrategy.OnPush is set #33

Closed
hpop opened this issue Feb 12, 2016 · 10 comments
Closed

Comments

@hpop
Copy link

hpop commented Feb 12, 2016

Hi,

I am currently try to use ChangeDetectionStrategy.OnPush in all my components. That means, that all bindings are not checked if the state of my application stays the same. Thats great but it means that all translate pipes are not getting re-evaluated if I change the current language.

I think It may possible to wrap the pipe into an own directive that listens to the onLangChange event and invalidates itself.

Do you have any Ideas?

@ocombe
Copy link
Collaborator

ocombe commented Feb 12, 2016

I'm not expert enough in Angular 2 to know the right thing to do here. Do you have an article somewhere explaining the different change detection strategies ?
And an example app that uses OnPush ?

@hpop
Copy link
Author

hpop commented Feb 12, 2016

I am just starting to figure it out as well and the documentation is not very good at this point of time.

I guess the best articles to start are:
http://victorsavkin.com/post/110170125256/change-detection-in-angular-2
http://victorsavkin.com/post/137821436516/managing-state-in-angular-2-applications

Give me some time and I will prepare a example for the case.

@ocombe
Copy link
Collaborator

ocombe commented Feb 12, 2016

Thanks, I'll look at them this week end

@hpop
Copy link
Author

hpop commented Feb 13, 2016

I have a hard time to get this running for an example. Do you have a working jsfiddle or plunker with ng2-translate?

In the meantime I tested #31 and it perfectly fixes my issue. If I use directives instead of pipes in my application, everything gets updated perfectly even if OnPush is used.

@sclausen
Copy link

@hpop but be aware, that you should be careful with html injection, since no sanitization will be performed.
I did a little bit of research because of @ocombe s concerns in #31, but I didn't found any lightweight solution for this problem.

@ocombe
Copy link
Collaborator

ocombe commented Feb 21, 2016

It look like we might need to write a sanitization library for ng2

@ocombe
Copy link
Collaborator

ocombe commented Mar 15, 2016

Hey @PascalPrecht any idea on how to make the pipe work with OnPush ?

@0x-r4bbit
Copy link

I don't know if there's any event system implemented in ng2-translate yet, but if there's something like a translate service, it could expose an observable which fires when the languages changes. Then the pipe could subscribe to that service and it could use the changedetector ref to mark the path til root to be checked.

@ocombe
Copy link
Collaborator

ocombe commented Mar 15, 2016

Thanks @PascalPrecht, it turns out that you can inject a ChangeDetectorRef in a pipe (I found out by reading the AsyncPipe source code). It should work now !

@baio
Copy link

baio commented Oct 15, 2018

Honestly ngxtranslate doesn't play well with OnPush change detection, for every component which uses translate pipe you has to write a lot of same boilerplate code, so think twice before use this lib if you want use it alongside with OnPush.

Has to write some helper pipe to workaround this https://gist.github.com/baio/e1da9826806ae3bad639b194958cbdcc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants