Skip to content

Instantly share code, notes, and snippets.

@Mustafa-Omran
Last active February 21, 2022 08:55
Show Gist options
  • Save Mustafa-Omran/834b6066933ca72a9598e8c39eaf1694 to your computer and use it in GitHub Desktop.
Save Mustafa-Omran/834b6066933ca72a9598e8c39eaf1694 to your computer and use it in GitHub Desktop.
Angular -custom directive for dynamic direction (rtl or ltr)
@Directive({
selector: '[setDirection]'
})
export class SetDirDirective {
/**
*
* @param el
* @param languageService
* @param locale
*/
constructor(private element: ElementRef,
private languageService: LanguageService,
) {
this.onChangeLanguage();
}
onChangeLanguage() {
this.languageService.language.subscribe((languageCode:string) => {
switch (languageCode) {
case 'ar':
this.element.nativeElement.style.direction = 'rtl';
this.element.nativeElement.style.textAlign = 'right';
break;
default:
this.element.nativeElement.style.direction = 'ltr';
this.element.nativeElement.style.textAlign = 'left';
break;
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment