javascript - 无法以 Angular 获取在 ngOnInit() 中动态生成的数组的长度

标签 javascript typescript angular5

无法获取已在 angular 的 ngOnInit() 中动态生成的数组的长度。

@Component({
  selector: 'rise-our-champions',
  templateUrl: './our-champions.component.html',
  styleUrls: ['./our-champions.component.css']
})
export class OurChampionsComponent implements OnInit {

champs: any = [];

ngOnInit() {
    this.getChampions(0, 12, 'created_at', 'desc', this.campaignId); 
    console.log(this.champs);
    console.log(this.champs.length); //<================= This displays 0 where as my array populates with 4 values.
  }

  getChampions(offset: any, size: any, sort: any, order: any, id: any) {

    this._restApiService.getCampaignChampion(offset, size, sort, order, id).subscribe(
  data => {
    // this.champions = data['champions']
    data['champions'].forEach((champion: any) => {
      this.champs.push(champion);
    });

    if (data['champions'].length < 12) {
      this.showMore = false;
    } else {
      this.showMore = true;
    }
  },
  error => {
    if (error.status == 400) {
      this.router.navigate(['/error']);
    } else {
      this.errorMessage = <any>error,
        this.missionService.announceMissionToShowCustomAlert({
          requestType: FeatureType.showCustomAlert,
          title: 'Error',
          message: '<p>' + this.errorMessage + '</p>',
          redirectType: FeatureType.isError
        })
    }
  },
  () => {

  }
);

我调用 ngOnInit() 生命周期钩子(Hook)中的函数,它显示以下输出,如何获取数组的长度? 请看下面的截图: enter image description here

最佳答案

由于 async 调用,您的 console.log() 会在值出现之前打印出来。将 console.log() 放入您的 getChampions() 函数中,如下所示:

getChampions(offset: any, size: any, sort: any, order: any, id: any) {
  this._restApiService.getCampaignChampion(offset, size, sort, order, id).subscribe(
    data => {
      // this.champions = data['champions']
      data['champions'].forEach((champion: any) => {
      this.champs.push(champion);
    });

    if (data['champions'].length < 12) {
      this.showMore = false;
    } else {
      this.showMore = true;
    }
  },
  error => {
    if (error.status == 400) {
      this.router.navigate(['/error']);
    } else {
      this.errorMessage = <any>error,
        this.missionService.announceMissionToShowCustomAlert({
          requestType: FeatureType.showCustomAlert,
          title: 'Error',
          message: '<p>' + this.errorMessage + '</p>',
          redirectType: FeatureType.isError
        })
    }
  },
  () => {
     // THIS IS EXECUTED AFTER THE SUBSCRIBE COMPLETES
     console.log(this.champions);
  }
}

关于javascript - 无法以 Angular 获取在 ngOnInit() 中动态生成的数组的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49994485/

相关文章:

javascript - 在具有相同配置的多个 Angular 应用程序中外部化服务配置

typescript - 我如何在 Vue 3 中使用 vite-plugin-pwa 实现 typescript service worker

Angular 传递将数据解析为子路由

javascript - Array.prototype.concat 的行为不像预期的那样

javascript网格按数字排序,排序问题

javascript - 等待音乐加载时的 ionic 旋转器( ionic 2/Angular 2)

javascript - TypeScript:自动生成的动态函数名称

angular5 - Angular 5,将 SVG 文件导入组件

angular-cli - ng 在子目录中生成组件

javascript - 无法使用 'in' 运算符在中搜索 '_id'