forked from angular/components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.ts
40 lines (35 loc) · 1.35 KB
/
error.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {Attribute, Directive, ElementRef, InjectionToken, Input} from '@angular/core';
let nextUniqueId = 0;
/**
* Injection token that can be used to reference instances of `MatError`. It serves as
* alternative token to the actual `MatError` class which could cause unnecessary
* retention of the class and its directive metadata.
*/
export const MAT_ERROR = new InjectionToken<MatError>('MatError');
/** Single error message to be shown underneath the form-field. */
@Directive({
selector: 'mat-error, [matError]',
host: {
'class': 'mat-mdc-form-field-error mat-mdc-form-field-bottom-align',
'aria-atomic': 'true',
'[id]': 'id',
},
providers: [{provide: MAT_ERROR, useExisting: MatError}],
})
export class MatError {
@Input() id: string = `mat-mdc-error-${nextUniqueId++}`;
constructor(@Attribute('aria-live') ariaLive: string, elementRef: ElementRef) {
// If no aria-live value is set add 'polite' as a default. This is preferred over setting
// role='alert' so that screen readers do not interrupt the current task to read this aloud.
if (!ariaLive) {
elementRef.nativeElement.setAttribute('aria-live', 'polite');
}
}
}