autocompletar no funciona en angular?
Quiero hacer el cuadro de texto Autocompletar para los datos a continuación
Elements = [
{ id: 1, name: 'Hydrogen' },
{ id: 2, name: 'Helium' },
{ id: 3, name: 'Lithium' }
];
HTML
<mat-form-field class="example-full-width">
<input type="text" placeholder="Pick one" aria-label="Number" matInput [formControl]="name"
[matAutocomplete]="auto" [errorStateMatcher]="matcher">
<mat-autocomplete autoActiveFirstOption #auto="matAutocomplete" (selectionChange)="elementSelectionChange($event)">
<mat-option *ngFor="let Element of filteredOptions | async" [value]="Element.name">
{{ Element.name}}
</mat-option>
</mat-autocomplete>
<mat-error *ngIf="myForm.hasError('required', 'name')">Please choose an name</mat-error>
</mat-form-field>
componente
export class DialogOverviewExampleDialog implements OnInit{
Elements = [
{ id: 1, name: 'Hydrogen' },
{ id: 2, name: 'Helium' },
{ id: 3, name: 'Lithium' }
];
matcher = new MyErrorStateMatcher();
selectedElementSymbol: any;
myForm: FormGroup;
symbol;
//name;
name: FormControl = new FormControl();
id;
filteredOptions: Observable<string[]>;
OnInit() {
this.filteredOptions = this.name.valueChanges.pipe(
startWith(''),
map(val => this.filter(val))
);
}
filter(val: any): any[] {
return this.Elements.filter(Element => {
return Element.name.toLowerCase().indexOf(val.toLowerCase()) > -1;
});
}
constructor(
public dialogRef: MatDialogRef<DialogOverviewExampleDialog>,
@Inject(MAT_DIALOG_DATA) public data: any,
private formBuilder: FormBuilder) {
}
}
Esto es lo que construí hasta ahora. autocompletar no funciona, pero alguien me ayudó a seguir adelante