I'm using Angular Material "^6.4.7" and Angular "^6.1.9". I done some routing to navigate through a menu filled with observables (to navigate by id of these observables). Now I have to add an autocomplete to filter this menu (to help the user to find a particular option), but I have some trouble to achieve this.
My html
<mat-form-field>
<mat-label>Find datasource...</mat-label>
<input matInput name="datasource filter" [formControl]="searchForm" [ngModel]="dataSourceFilterInput">
</mat-form-field>
<div *ngFor="let datasource of dataSourceFiltered$ | async">
<div [routerLink]="[datasource.id]">
<h6>{{datasource.name}}</h6>
</div>
</div>
My ts:
export class DataSourceSelectorComponent implements OnInit {
dataSourceFiltered$: Observable<IDataSourceDefinition[]>;
dataSource$: Observable<IDataSourceDefinition[]>;
dataSourceList: IDataSourceDefinition[] = [];
searchForm: FormControl = new FormControl();
constructor(private _datasourceService: DataSourceConfigurationService, private _route: ActivatedRoute) {
}
ngOnInit() {
this.dataSourceFiltered$ = this._route.paramMap.pipe(
switchMap(params => {
this.selectedId = +params.get("id");
this.dataSource$ = this._datasourceService.getAllDataSources();
this.dataSource$.subscribe(ds => (this.dataSourceList = ds));
return this.dataSource$;
})
);
}
}
Where IDataSourceDefinition is my interface with the property "name".
I think that inside the ngOnInit I have to add the filter (as suggested in Angular Material using a form control (searchForm in this case) and the "valueChanges.pipe()"), but all the methods like "map" or "startWith" or "filter" cannot be used with Observables.
I cannot just filter the subscription of the Observable, I need that the filter returns an Observable, otherwise the routing will break.
Any suggestion?