@@ -6,9 +6,10 @@ Start by adding a regular `matInput` to your template. Let's assume you're using
6
6
directive from ` ReactiveFormsModule ` to track the value of the input.
7
7
8
8
> Note: It is possible to use template-driven forms instead, if you prefer. We use reactive forms
9
- in this example because it makes subscribing to changes in the input's value easy. For this example, be sure to
10
- import ` ReactiveFormsModule ` from ` @angular/forms ` into your ` NgModule ` . If you are unfamiliar with using reactive
11
- forms, you can read more about the subject in the [ Angular documentation] ( https://angular.io/guide/reactive-forms ) .
9
+ in this example because it makes subscribing to changes in the input's value easy. For this
10
+ example, be sure to import ` ReactiveFormsModule ` from ` @angular/forms ` into your ` NgModule ` .
11
+ If you are unfamiliar with using reactive forms, you can read more about the subject in the
12
+ [ Angular documentation] ( https://angular.io/guide/reactive-forms ) .
12
13
13
14
* my-comp.html*
14
15
``` html
@@ -41,9 +42,7 @@ to the input's `matAutocomplete` property.
41
42
</mat-form-field >
42
43
43
44
<mat-autocomplete #auto =" matAutocomplete" >
44
- <mat-option *ngFor =" let option of options" [value] =" option" >
45
- {{ option }}
46
- </mat-option >
45
+ <mat-option *ngFor =" let option of options" [value] =" option" >{{option}}</mat-option >
47
46
</mat-autocomplete >
48
47
```
49
48
@@ -61,8 +60,8 @@ option's first letter. We already have access to the built-in `valueChanges` Obs
61
60
them through this filter. The resulting Observable, ` filteredOptions ` , can be added to the
62
61
template in place of the ` options ` property using the ` async ` pipe.
63
62
64
- Below we are also priming our value change stream with an empty string so that the options are filtered by
65
- that value on init (before there are any value changes).
63
+ Below we are also priming our value change stream with an empty string so that the options are
64
+ filtered by that value on init (before there are any value changes).
66
65
67
66
\* For optimal accessibility, you may want to consider adding text guidance on the page to explain
68
67
filter criteria. This is especially helpful for screenreader users if you're using a non-standard
@@ -91,6 +90,27 @@ injection token.
91
90
92
91
<!-- example(autocomplete-auto-active-first-option) -->
93
92
93
+ ### Attaching the autocomplete panel to a different element
94
+
95
+ By default the autocomplete panel will be attached to your input element, however in some cases you
96
+ may want it to attach to a different container element. You can change the element that the
97
+ autocomplete is attached to using the ` matAutocompleteOrigin ` directive together with the
98
+ ` matAutocompleteConnectedTo ` input:
99
+
100
+ ``` html
101
+ <div class =" custom-wrapper-example" matAutocompleteOrigin #origin =" matAutocompleteOrigin" >
102
+ <input
103
+ matInput
104
+ [formControl] =" myControl"
105
+ [matAutocomplete] =" auto"
106
+ [matAutocompleteConnectedTo] =" origin" >
107
+ </div >
108
+
109
+ <mat-autocomplete #auto =" matAutocomplete" >
110
+ <mat-option *ngFor =" let option of options" [value] =" option" >{{option}}</mat-option >
111
+ </mat-autocomplete >
112
+ ```
113
+
94
114
### Keyboard interaction
95
115
- <kbd >DOWN_ARROW</kbd >: Next option becomes active.
96
116
- <kbd >UP_ARROW</kbd >: Previous option becomes active.
@@ -105,7 +125,7 @@ injection token.
105
125
<mat-autocomplete #auto =" matAutocomplete" >
106
126
<mat-optgroup *ngFor =" let group of filteredGroups | async" [label] =" group.name" >
107
127
<mat-option *ngFor =" let option of group.options" [value] =" option" >
108
- {{ option.name }}
128
+ {{option.name}}
109
129
</mat-option >
110
130
</mat-optgroup >
111
131
</mat-autocomplete >
@@ -115,5 +135,5 @@ injection token.
115
135
The input for an autocomplete without text or labels should be given a meaningful label via
116
136
` aria-label ` or ` aria-labelledby ` .
117
137
118
- The autocomplete trigger is given ` role="combobox" ` . The trigger sets ` aria-owns ` to the autocomplete's
119
- id, and sets ` aria-activedescendant ` to the active option's id.
138
+ The autocomplete trigger is given ` role="combobox" ` . The trigger sets ` aria-owns ` to the
139
+ autocomplete's id, and sets ` aria-activedescendant ` to the active option's id.
0 commit comments