@@ -2,51 +2,31 @@ The autocomplete is a normal text input enhanced by a panel of suggested options
22
33### Simple autocomplete
44
5- Start by adding a regular ` matInput ` to your template. Let's assume you're using the ` formControl `
6- directive from ` ReactiveFormsModule ` to track the value of the input.
5+ Start by creating the autocomplete panel and the options displayed inside it. Each option should be
6+ defined by a ` mat-option ` tag. Set each option's value property to whatever you'd like the value
7+ of the text input to be when that option is selected.
8+
9+ <!-- example({"example":"autocomplete-simple",
10+ "file":"autocomplete-simple-example.html",
11+ "region":"mat-autocomplete"}) -->
12+
13+ Next, create the input and set the ` matAutocomplete ` input to refer to the template reference we assigned
14+ to the autocomplete. Let's assume you're using the ` formControl ` directive from ` ReactiveFormsModule ` to
15+ track the value of the input.
716
817> Note: It is possible to use template-driven forms instead, if you prefer. We use reactive forms
918 in this example because it makes subscribing to changes in the input's value easy. For this
1019example, be sure to import ` ReactiveFormsModule ` from ` @angular/forms ` into your ` NgModule ` .
1120If you are unfamiliar with using reactive forms, you can read more about the subject in the
1221[ Angular documentation] ( https://angular.io/guide/reactive-forms ) .
1322
14- * my-comp.html*
15- ``` html
16- <mat-form-field >
17- <input type =" text" matInput [formControl] =" myControl" >
18- </mat-form-field >
19- ```
20-
21- Next, create the autocomplete panel and the options displayed inside it. Each option should be
22- defined by a ` mat-option ` tag. Set each option's value property to whatever you'd like the value
23- of the text input to be upon that option's selection.
24-
25- * my-comp.html*
26- ``` html
27- <mat-autocomplete >
28- <mat-option *ngFor =" let option of options" [value] =" option" >
29- {{ option }}
30- </mat-option >
31- </mat-autocomplete >
32- ```
33-
3423Now we'll need to link the text input to its panel. We can do this by exporting the autocomplete
3524panel instance into a local template variable (here we called it "auto"), and binding that variable
3625to the input's ` matAutocomplete ` property.
3726
38- * my-comp.html*
39- ``` html
40- <mat-form-field >
41- <input type =" text" matInput [formControl] =" myControl" [matAutocomplete] =" auto" >
42- </mat-form-field >
43-
44- <mat-autocomplete #auto =" matAutocomplete" >
45- <mat-option *ngFor =" let option of options" [value] =" option" >{{option}}</mat-option >
46- </mat-autocomplete >
47- ```
48-
49- <!-- example(autocomplete-simple) -->
27+ <!-- example({"example":"autocomplete-simple",
28+ "file":"autocomplete-simple-example.html",
29+ "region":"input"}) -->
5030
5131### Adding a custom filter
5232
@@ -127,18 +107,9 @@ autocomplete is attached to using the `matAutocompleteOrigin` directive together
127107
128108### Option groups
129109` mat-option ` can be collected into groups using the ` mat-optgroup ` element:
130- <!-- example(autocomplete-optgroup) -->
131-
132-
133- ``` html
134- <mat-autocomplete #auto =" matAutocomplete" >
135- <mat-optgroup *ngFor =" let group of filteredGroups | async" [label] =" group.name" >
136- <mat-option *ngFor =" let option of group.options" [value] =" option" >
137- {{option.name}}
138- </mat-option >
139- </mat-optgroup >
140- </mat-autocomplete >
141- ```
110+ <!-- example({"example":"autocomplete-optgroup",
111+ "file":"autocomplete-optgroup-example.html",
112+ "region":"mat-autocomplete"}) -->
142113
143114### Accessibility
144115The input for an autocomplete without text or labels should be given a meaningful label via
0 commit comments