1
1
The autocomplete is a normal text input enhanced by a panel of suggested options.
2
- You can read more about autocompletes in the [ Material Design spec] ( https://material.io/guidelines/components/text-fields.html#text-fields-auto-complete-text-field ) .
3
2
4
3
### Simple autocomplete
5
4
@@ -14,7 +13,7 @@ forms, you can read more about the subject in the [Angular documentation](https:
14
13
* my-comp.html*
15
14
``` html
16
15
<mat-form-field >
17
- <input type =" text" matInput [formControl] =" myControl" >
16
+ <input type =" text" matInput [formControl] =" myControl" >
18
17
</mat-form-field >
19
18
```
20
19
@@ -25,9 +24,9 @@ of the text input to be upon that option's selection.
25
24
* my-comp.html*
26
25
``` html
27
26
<mat-autocomplete >
28
- <mat-option *ngFor =" let option of options" [value] =" option" >
29
- {{ option }}
30
- </mat-option >
27
+ <mat-option *ngFor =" let option of options" [value] =" option" >
28
+ {{ option }}
29
+ </mat-option >
31
30
</mat-autocomplete >
32
31
```
33
32
@@ -38,13 +37,13 @@ to the input's `matAutocomplete` property.
38
37
* my-comp.html*
39
38
``` html
40
39
<mat-form-field >
41
- <input type =" text" matInput [formControl] =" myControl" [matAutocomplete] =" auto" >
40
+ <input type =" text" matInput [formControl] =" myControl" [matAutocomplete] =" auto" >
42
41
</mat-form-field >
43
42
44
43
<mat-autocomplete #auto =" matAutocomplete" >
45
- <mat-option *ngFor =" let option of options" [value] =" option" >
46
- {{ option }}
47
- </mat-option >
44
+ <mat-option *ngFor =" let option of options" [value] =" option" >
45
+ {{ option }}
46
+ </mat-option >
48
47
</mat-autocomplete >
49
48
```
50
49
@@ -55,17 +54,17 @@ to the input's `matAutocomplete` property.
55
54
At this point, the autocomplete panel should be toggleable on focus and options should be
56
55
selectable. But if we want our options to filter when we type, we need to add a custom filter.
57
56
58
- You can filter the options in any way you like based on the text input* . Here we will perform a
57
+ You can filter the options in any way you like based on the text input\ * . Here we will perform a
59
58
simple string test on the option value to see if it matches the input value, starting from the
60
- option's first letter. We already have access to the built-in ` valueChanges ` observable on the
59
+ option's first letter. We already have access to the built-in ` valueChanges ` Observable on the
61
60
` FormControl ` , so we can simply map the text input's values to the suggested options by passing
62
- them through this filter. The resulting observable ( ` filteredOptions ` ) can be added to the
61
+ them through this filter. The resulting Observable, ` filteredOptions ` , can be added to the
63
62
template in place of the ` options ` property using the ` async ` pipe.
64
63
65
64
Below we are also priming our value change stream with ` null ` so that the options are filtered by
66
65
that value on init (before there are any value changes).
67
66
68
- * For optimal accessibility, you may want to consider adding text guidance on the page to explain
67
+ \ * For optimal accessibility, you may want to consider adding text guidance on the page to explain
69
68
filter criteria. This is especially helpful for screenreader users if you're using a non-standard
70
69
filter that doesn't limit matches to the beginning of the string.
71
70
@@ -74,7 +73,7 @@ filter that doesn't limit matches to the beginning of the string.
74
73
### Setting separate control and display values
75
74
76
75
If you want the option's control value (what is saved in the form) to be different than the option's
77
- display value (what is displayed in the actual text field), you'll need to set the ` displayWith `
76
+ display value (what is displayed in the text field), you'll need to set the ` displayWith `
78
77
property on your autocomplete element. A common use case for this might be if you want to save your
79
78
data as an object, but display just one of the option's string properties.
80
79
@@ -88,7 +87,7 @@ desired display value. Then bind it to the autocomplete's `displayWith` property
88
87
- <kbd >UP_ARROW</kbd >: Previous option becomes active.
89
88
- <kbd >ENTER</kbd >: Select currently active item.
90
89
91
- #### Option groups
90
+ ### Option groups
92
91
` mat-option ` can be collected into groups using the ` mat-optgroup ` element:
93
92
94
93
``` html
@@ -102,8 +101,8 @@ desired display value. Then bind it to the autocomplete's `displayWith` property
102
101
```
103
102
104
103
### Accessibility
105
- The input for autocomplete without text or labels should be given a meaningful label via
104
+ The input for an autocomplete without text or labels should be given a meaningful label via
106
105
` aria-label ` or ` aria-labelledby ` .
107
106
108
- Autocomplete trigger is given ` role="combobox" ` . The trigger sets ` aria-owns ` to the autocomplete's
107
+ The autocomplete trigger is given ` role="combobox" ` . The trigger sets ` aria-owns ` to the autocomplete's
109
108
id, and sets ` aria-activedescendant ` to the active option's id.
0 commit comments