I'm creating a screen that uses a segment component to filter the results in a list of an observable using the async pipe from an http request. I'm going to create a pipe filter for this to clean it up as it's redundant, but I'm more concerned why it's happening the way is it - I'm new to observables. Everything display and works as expected, but when I toggle the segment filter at the top of the screen it sends a new request to the api each time and rebuilds the list based on the customer type. Is it because the *ngIf is based on the type property of the customer? It just seems a bit ridiculous to send a new request every time it switches. Also, if you could advise a better way to do this with a filter (or whatever method) it would be greatly appreciated.
Method in my service class:
/**
* Get a list of the user's customers
*/
list(): Observable<Customer[]> {
return this.authHttp.get<Customer[]>('/api/customers')
.map(customers => {
return customers.json().data
}).pipe(
catchError(this.handleError('getCustomers', []))
);
}
Method in my controller:
ionViewDidLoad() {
this.customers = this.customerService.list();
}
Segment component with model binding:
<ion-segment [(ngModel)]="type">
<ion-segment-button value="prospect">
Prospects
</ion-segment-button>
<ion-segment-button value="customer">
Customers
</ion-segment-button>
</ion-segment>
Content switching and async pipe:
<div [ngSwitch]="type">
<ion-list *ngSwitchCase="'prospect'">
<ng-container *ngFor="let customer of customers | async">
<button *ngIf="customer.type==='prospect'" ion-item (click)="customerSelected(customer)">
{{ customer.first_name }} {{ customer.last_name }}
</button>
</ng-container>
</ion-list>
<ion-list *ngSwitchCase="'customer'">
<ng-container *ngFor="let customer of customers | async">
<button ion-item *ngIf="customer.type==='customer'" (click)="customerSelected(customer)">
{{ customer.first_name }} {{ customer.last_name }}
</button>
</ng-container>
</ion-list>
</div>