I'm trying to make an autocomplete using Observables but I'm getting an error that says
Property 'filter' does not exist on type 'Observable<Entity[]>'
How can I make an autocomplete based on what I made? (I was thinking of using .pipe()
but I don't really understand it
Here is what I tried:
.ts
entity$: Observable<Entity>;
entities$: Observable<Entity[]>;
inputEntity = new FormControl();
autocompleteEntities: Entity[] = [];
subEntityText: Subscription;
constructor(
private userProvider: UserProvider,
private entityProvider: EntityProvider,
private cd: ChangeDetectorRef
) {}
ngOnInit() {
this.userProvider.user$.subscribe((user: User) => {
this.entities$ = this.entityProvider.getAll(user.uid); # GET ENTITIES BASED ON USER UID
});
this.subEntityText = this.inputEntity.valueChanges.subscribe((query: string) => {
if (query === '') {
this.entity$ = null;
} else {
this.autocompleteEntities = this.searchEntity(query);
this.cd.detectChanges();
}
});
}
searchEntity(query: string): Entity[] {
if (query === '') return [];
const queryRegExp = new RegExp(query, 'i');
return this.entities$
.filter(char => queryRegExp.test(char.name))
.sort((a, b) => a.name.length - b.name.length);
}
.html
<input
type="text"
[formControl]="inputEntity"
[matAutocomplete]="auto"
/>
<mat-autocomplete #auto="matAutocomplete" class="option-autocomplete" autoActiveFirstOption>
<mat-option class="option" *ngFor="let entity of autocompleteEntities" [value]="entity">
<div class="option-character">
<span class="option-character-name">{{entity.name}}</span>
</div>
</mat-option>
</mat-autocomplete>
Thanks in advance