首页 > 解决方案 > Angular:在函数和 api 调用之外访问变量

问题描述

Angular 和 JS 的超级新手,运行版本 10,你到底如何在服务调用后获取数组数据?任何有关如何执行此操作的提示将不胜感激。数组在调用内部是完整的,但从不在外部。我怎样才能得到它,以便我可以在我的 html 模板中使用该数组?

// PROBLEM, as noted where undefined below.

selectScript(s_id, r_id, prompts: Prompt, scripts: Script){
  this.apiService.readScript(s_id).subscribe((scripts: Script[])=>{
    this.scripts = scripts;
   // console.log(scripts); // Array OK
  });
  this.apiService.readPrompts(r_id).subscribe((prompts: Prompt[])=>{
    this.prompts = prompts;
    //console.log(prompts); // Array OK
  });
  console.log(this.scripts); // Undefined! How to get array here?
  this.ud = true;
  this.ngOnInit();
}

标签: angular

解决方案


推荐阅读