0

How do I get the value set in the "subscribe" outside of it :

MyComponent.ts

 getLabelTypeOfDemandByCode(code: string) {
        const label;
        this.adminService.getLabelByCode(code).subscribe(
          labelByCode => {
            label = labelByCode.label;
            localStorage.setItem(code, JSON.stringify(labelByCode.label));
          });
         // Here when I check the value of label it is null
        console.log(label); // ==> null
        // Or when I try to get value of local storage 
        console.log(JSON.parse(localStorage.getItem("ABI")); // it is null
      }

How can I get the value outside the subscribe ?

user1814879
  • 984
  • 6
  • 23
  • 49
  • Yes but I want to get the value after the subscribe and not in the subscribe .. It is possible ? – user1814879 Aug 17 '17 at 09:33
  • No because this is an async operation. You can also check the new async/await syntax, might be easier for you to implement: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function – eko Aug 17 '17 at 10:04

0 Answers0