0

Good day,

I'm new with using Angular6, I'm having a trouble because I want to get the value response result inside the subscribe and use it outside the subscribe.

Here are my codes:

name: string;

constructor(private myService: MyService) { }

myMethod(){
    this.myService.getName().subscribe((response: any) => {
        this.name = response.name;
    }, error => {
        console.log(`Something went wrong`);
    });

    console.log(this.name);
}

To add up,

Here is my sample service:

myService(): Observable<any> {
    return this.http.get<any>(`${apiUrl}names/`);
}

Unfortunately, I'm having undefined error on my console.log(this.name) I already using Observable on my angular service

jsonGPPD
  • 987
  • 4
  • 16
  • 31

0 Answers0