Reputation: 223
I have created an autocomplete controls with http api
getting data.
But <mat-option *ngFor="let region of regions" [value]="region.name">
is throw an error like this:
Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
I have checked the data returns Region[]
in method 1
, line 52.
And I google for solution method 2
, it works but not able to work with loading indicator
I hope someone can give a hand to me. Thanks.
My code: https://stackblitz.com/edit/angular-89p1pm
I have 3 questions to ask:
Observable with loading indicator
form.get('region').valueChanges.pipe(
debounceTime(300),
tap(r => this.isLoading = true),
switchMap(inputCountry =>
this.regionService.getRegions().pipe(
map( regions => {
return regions.filter(region => region.name.indexOf(inputCountry) >= 0)
})
)
)
).subscribe(data => {
console.log('onRegionChanged: data', data);
this.isLoading = false;
this.regions = data;
})
form.get('region').valueChanges.pipe(
debounceTime(300),
tap(r => this.isLoading$.next(true)),
).subscribe(inputCountry => {
this.regions$ = this.regionService.getRegions().pipe(
tap(r => this.isLoading$.next(false)),
map(regions => regions.filter(region => region.name.indexOf(inputCountry) >= 0))
)
});
Upvotes: 0
Views: 85
Reputation:
Your issue is that you're iterating over an empty array. Just add it to your condition.
<ng-container *ngIf="!isLoading && regions?.length">
Upvotes: 1