0
I have in my project a field that I need to use the autocomplete. But it’s not working. Could someone help me?
Component
filteredCostCenter: Observable<CentroCusto[]>;
ngOnInit() {
this.filteredCostCenter = this.form.controls.centroCustoId.valueChanges.pipe(
startWith(null),
debounceTime(200),
distinctUntilChanged(),
switchMap(value => {
return this.searchCostsCenters(value || '')
})
);
}
private searchCostsCenters(value: string): Observable<CentroCusto[]> {
return this.centroCustoService.getWithPages(1,30,'id','asc', value).pipe(
map(response => response.results.filter(opt => {
return opt.nome.toLowerCase().indexOf(value.toLowerCase())
})
));
};
HTML
<mat-form-field class="app-form-control inputPadding">
<input type="text" matInput placeholder="Centro de Custo" formControlName="centroCustoId"
aria-label="Number" [matAutocomplete]="auto">
<mat-autocomplete autoActiveFirstOption #auto="matAutocomplete">
<mat-option *ngFor="let result of filteredCostCenter | async" [value]="result">
{{ result }}
</mat-option>
</mat-autocomplete>
</mat-form-field>
Using this way, it does not work at all. I did it based on the example that exists on the angular material site.