Angular returns data-bound local variables

[setting.component.ts]

  TagList = [];

  public getTagList() {
    this.settingService.getTags()
      .subscribe((response) => {
        this.TagList = response.data;
        console.log(this.TagList);  //A
    });
    console.log(this.TagList);  //B
  }

setting.service.ts

public getTags(): Observable<any> {
     return this.http.get<any>(`${this.appConfig.apiUrl}/setting/setting/`, { withCredentials: true });
}

as shown in the figure, the data returned by background can be printed normally at A
but B is undefined, some friends say it is asynchronous, and B uses setTimeout as well as undefined. Thank you

for guidance.
Apr.29,2022

just figure out the asynchronous callback


well, it's asynchronous, but it's not clear why setTimeout (console.log (this.TagList)) still doesn't work. But don't worry about it, just go to html and use TagList.

Menu