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(); } } - How to get access to "locale" input of all directive elements and change it?
- How to create one instance of directive for all components? At the moment each component creates own instance which is not good.
hostComponentinside your ts file and the do whatever you want with his data.localeexists in host component. So inside your directive just inject thehostComponent. There you can findlocaleChange.