My page display a list of opportunities based on web service,
after that, i want to load more opportunities after clicking on the "Load More
" button, which i couldn't find the best way to keep the same observable 'opportunities
'
this is my code source :
import { Component, OnInit } from "@angular/core";
import { Observable } from "rxjs";
import { Opportunity } from "../../models/opportunity.model";
import { OpportunityService } from "../../services/opportunity.service";
@Component({
template : `
<div class="container-lg">
<mat-toolbar><span i18n>Opportunities</span></mat-toolbar>
<app-cards *ngIf="opportunities$ | async as opportunities; else searchLoading" [opportunities]="opportunities"></app-cards>
<button mat-raised-button (click)="loadOportunities()" color="primary">Load More</button>
<ng-template #searchLoading>
<div class="center"><mat-progress-spinner mode="indeterminate"></mat-progress-spinner></div>
</ng-template>
</div>
`,
styleUrls : ['opportunities.page.scss']
})
export class OpportunitiesPage implements OnInit {
offset: number = 0;
limit: number = 12;
opportunities$?: Observable<Opportunity[]> ;
constructor(private opportunityService : OpportunityService){
}
ngOnInit(): void {
this.opportunities$ = this.opportunityService.getSeacrhOpportunities(this.offset , this.limit);
}
loadOportunities(): void{
this.offset = this.offset+this.limit;
//TODO concat newest with oldest opportunities
}
}
for the opportunity service :
getSeacrhOpportunities(offset: number , limit : number): Observable<Opportunity[]> {
let param: any = {'offset': offset , 'limit' : limit , 'locale' : this.locale};
return this.httpClient.get<APIData<Opportunity[]>>(`${environment.apiUrl}/ws/opportunities`, {params: param}).pipe(
map((response) => response.data),
catchOffline(),
);
}