0

I have a lot of angular components and i need to pass to all of them one localisation @Input:

<mycomponent [locale]="locale" myDirective></mycomponent> …… <mycomponent [locale]="locale" myDirective></mycomponent>

The idea is to create a directive and subscribe inside to change locale service:

export class myDirective implements OnDestroy, OnInit { localeChange: Subscription; constructor(private translationService: TranslationService) {} ngOnInit() { this.localeChange = this.translationService .localeChanged .subscribe((lang: string) => { //set input 'locale' for all elements of this directive }); } ngOnDestroy() { this.localeChange.unsubscribe(); } }

  1. How to get access to "locale" input of all directive elements and change it?
  2. How to create one instance of directive for all components? At the moment each component creates own instance which is not good.
4
  • Is that helps with your problem? stackoverflow.com/questions/46014761/… . You can get your hostComponentinside your ts file and the do whatever you want with his data. Commented Oct 1, 2020 at 8:00
  • Sorry i did not mention that there are different components, not the same ones. Commented Oct 1, 2020 at 8:05
  • That does not chance anything. locale exists in host component. So inside your directive just inject the hostComponent. There you can find localeChange. Commented Oct 1, 2020 at 8:11
  • In your example injected "MyComponent" is a predifined component, right? p.s. I do not need to find "localeChange", this used in directive. Commented Oct 1, 2020 at 8:28

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.