Angular http request return has no return value

  public getList () {
    this.http.get("lottery/activity/activity-list", {
      // params: {pageSize: "20"},
    })
    .subscribe((res: any) => {
      if (res.resultCode == 200) {
        this.activityList = res.resultData;
        console.log(this.activityList);//
        return res.resultData; //tsservice 
      }
      // 
    }, (err: any) => {
    });
  }
  public OnInit () {
    this.activityList = this.activityService.getList();
    console.log(this.activityList);//
  }
Mar.16,2021
The

request is asynchronous, and your Synchronize code in OnInit will not print out naturally.


  1. first of all, it is useless for you to use return in subscribe in an observable.
  2. secondly, if your activityList is an array, you can use a more advanced usage if you traverse the generated activity through this array. Take advantage of AsyncPipe
    <ng-container *ngFor="let activity of activityList | async">
        <activity [options]="activity"></activity>
    </ng-container>
    
    public activityList: Observable<Array<any>>;
    
    ngOnInit() {
        this.activityList = 
            this.http.get('lottery/activity/activity-list', {params: {pageSize: '20'} });
    }
Menu