The dir
attribute
is typically applied to <html>
or <body>
element of a page. However, it can be used on any
element, within your Angular app, to apply a text-direction to a smaller subset of the page.
All Angular Material components automatically reflect the LTR/RTL direction of their container.
@angular/cdk/bidi
provides a Directionality
injectable that can be used by any component
in your application. To consume it, you must import BidiModule
from @angular/cdk/bidi
.
Directionality
provides two useful properties:
value
: the current text direction, either'ltr'
or'rtl'
.change
: anObservable
that emits whenever the text-direction changes. Note that this only captures changes fromdir
attributes inside the Angular application context. It will not emit for changes todir
on<html>
and<body>
, as they are assumed to be static.
@Component({ /* ... */ })
export class MyCustomComponent {
private dir: Direction;
constructor(directionality: Directionality) {
this.dir = directionality.value;
directionality.change.subscribe(() => {
this.dir = directionality.value;
});
}
}