I'm using ngrx store to record state, my state currently holds my list of accounts and the current page (for paging).
On my list of accounts component i call the store to get the current page and pass that to a service (web api) to get the list of accounts.
this.currentPage$ = this.store.select(getCurrentPage);
My angular service is expecting a variable (currentPage) but as a type of number, the store select returns an Observable.
getListOfCustomer(currentPage: number): Observable<ListDto<CustomerList>> {}
My variable this.currentPage$
is currently a type of Observable<number>
How can i convert an
Observable<number>
to a number
to pass it to my service?
subscribe to get result as number:
currentPageSub :Subscription;
...
this.currentPageSub = this.store.select(getCurrentPage).subscribe(
(page: number) => {
this.currentPage$=page;
}
);
You can create an async function and convert the observable to a promise and await it
async method(){
var pageNr = await this.currentPage$.toPromise();
this.getListOfCustomer(pageNr);
}
来源:https://stackoverflow.com/questions/47013653/angular-convert-a-observablenumber-to-a-number