Search code examples
angulartypescriptngrxngrx-store

Angular - Convert a Observable<number> to a number


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?


Solution

  • subscribe to get result as number:

    let currentPageSub :Subscription;
    ...
    this.currentPageSub = this.store.select(getCurrentPage).subscribe(
     (page: number) => {
         this.currentPage$=page;
     }
    );