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.
trigger
  • 489
  • 10
  • 26
  • Is that helps with your problem? https://stackoverflow.com/questions/46014761/how-to-access-host-component-from-directive . You can get your `hostComponent`inside your ts file and the do whatever you want with his data. – StPaulis Oct 01 '20 at 08:00
  • Sorry i did not mention that there are different components, not the same ones. – trigger Oct 01 '20 at 08:05
  • That does not chance anything. `locale` exists in host component. So inside your directive just inject the `hostComponent`. There you can find `localeChange`. – StPaulis Oct 01 '20 at 08:11
  • In your example injected "MyComponent" is a predifined component, right? p.s. I do not need to find "localeChange", this used in directive. – trigger Oct 01 '20 at 08:28

0 Answers0