I use corner 5 and material 2.
In the TS file, I have this property:
filteredOptions: Observable<any[]>;
This property will have an array of values to display in the autocomplete field.
[{
id:'1', name: 'teste 1'},
{id:'2', name: 'teste 2'},
{id:'3', name: 'teste 3'
}]
This array of values comes from the database, and it will be shown after the user enters something.
HTML file:
## <form class="example-form">
## <mat-form-field class="example-full-width">
## <input type="text" placeholder="Assignee" aria-label="Assignee" matInput [formControl]="myControl" [matAutocomplete]="auto">
## <mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn">
## <mat-option *ngFor="let option of filteredOptions | async" [value]="option">
## {{ option.name }}
## </mat-option>
## </mat-autocomplete>
## </mat-form-field>
## </form>
Example TS file:
this.filteredOptions = Observable<any[]>;
ngOnInit() {
this.filteredOptions = this.myControl.valueChanges
.pipe(
startWith({}),
map(getArrayOfValue(val))
);
}
getArrayOfValue(val): Observable<any[]> {
const url = this.ROOT_URL + 'veiculo/' + val;
return this.http.get<any[]>(url);
}
This code gives me an error
ERROR in src / app / oficina / cadastro / veiculo / veiculo.component.ts (55,5): TS2322 error: type "Observable>" cannot be assigned to type "Observable". The type "Observable" cannot be assigned to the type "any []". The include property is not in the Observable type.
source
share