I'm trying get a "payload" from localstorage before than execute services method. Actualy i'm trying to do it on constructor method, but sometimes the variable "this.payload" isn't setted when a call a method.
This is my code
import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import * as Globals from '../../globals'; import { Observable, of } from 'rxjs'; import { LocalStorage } from '@ngx-pwa/local-storage'; @Injectable({ providedIn: 'root' }) export class RechargesService { public payload; constructor( private http: HttpClient, public localStorage: LocalStorage ) { this.localStorage.getItem("payload").subscribe((payload) => { this.payload = payload; }) } list(): Observable<any> { return this.http.post(Globals.url_api + "recharges/list", { // Sometimes this.payload isn't setted, i don't know why "payload": this.payload }); } } What's the correct way to do it? I know that in controller, i shouldn't write anything on constructor method, but in service it's correct?
list():function likelist(payload)