2017-07-13 20:23:47 +03:00
|
|
|
# Angular Material bi-directionality
|
|
|
|
|
|
|
|
|
|
### Setting a text-direction for your application
|
|
|
|
|
The [`dir` attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/dir)
|
|
|
|
|
is typically applied to `<html>` or `<body>` element of a page. However, it can be used on any
|
2018-02-22 00:48:20 +01:00
|
|
|
element, within your Angular app, to apply a text-direction to a smaller subset of the page.
|
2017-07-13 20:23:47 +03:00
|
|
|
|
|
|
|
|
All Angular Material components automatically reflect the LTR/RTL direction
|
2018-02-22 00:48:20 +01:00
|
|
|
of their container.
|
2017-07-13 20:23:47 +03:00
|
|
|
|
|
|
|
|
### Reading the text-direction in your own components
|
2017-09-28 18:32:42 -04:00
|
|
|
`@angular/cdk/bidi` provides a `Directionality` injectable that can be used by any component
|
2018-02-22 00:48:20 +01:00
|
|
|
in your application. To consume it, you must import `BidiModule` from `@angular/cdk/bidi`.
|
2017-07-13 20:23:47 +03:00
|
|
|
|
|
|
|
|
`Directionality` provides two useful properties:
|
|
|
|
|
* `value`: the current text direction, either `'ltr'` or `'rtl'`.
|
|
|
|
|
* `change`: an `Observable` that emits whenever the text-direction changes. Note that this only
|
|
|
|
|
captures changes from `dir` attributes _inside the Angular application context_. It will not
|
2018-02-22 00:48:20 +01:00
|
|
|
emit for changes to `dir` on `<html>` and `<body>`, as they are assumed to be static.
|
2017-07-13 20:23:47 +03:00
|
|
|
|
|
|
|
|
#### Example
|
|
|
|
|
```ts
|
2018-02-22 00:48:20 +01:00
|
|
|
@Component({ /* ... */ })
|
2017-07-13 20:23:47 +03:00
|
|
|
export class MyCustomComponent {
|
|
|
|
|
private dir: Direction;
|
|
|
|
|
|
|
|
|
|
constructor(directionality: Directionality) {
|
|
|
|
|
this.dir = directionality.value;
|
2018-02-22 00:48:20 +01:00
|
|
|
|
2017-07-13 20:23:47 +03:00
|
|
|
directionality.change.subscribe(() => {
|
|
|
|
|
this.dir = directionality.value;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-28 18:32:42 -04:00
|
|
|
```
|