Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何从通过http.get带来的数组中获取元素?_Angular_Typescript_Asynchronous - Fatal编程技术网

Angular 如何从通过http.get带来的数组中获取元素?

Angular 如何从通过http.get带来的数组中获取元素?,angular,typescript,asynchronous,Angular,Typescript,Asynchronous,我在Angular中通过http get请求带来了一系列问题对象。现在我无法从中获取元素:无法读取未定义的的属性“0”。我的猜测是,请求是异步的,当我尝试获取元素时,它不会加载 这是我的组件,我从中获取问题数组,并尝试获取当前问题: 从'@angular/common'导入{formatDate}; 从“@angular/core”导入{Component,OnInit}; 从“rxjs”导入{Observable}; 从'src/app/_models/IndividualSession'导

我在Angular中通过http get请求带来了一系列问题对象。现在我无法从中获取元素:
无法读取未定义的
的属性“0”。我的猜测是,请求是异步的,当我尝试获取元素时,它不会加载

这是我的组件,我从中获取问题数组,并尝试获取当前问题:

从'@angular/common'导入{formatDate};
从“@angular/core”导入{Component,OnInit};
从“rxjs”导入{Observable};
从'src/app/_models/IndividualSession'导入{IndividualSession};
从'src/app/_models/Question'导入{Question};
从'src/app/_models/User'导入{User};
从'src/app/_services/account.service'导入{AccountService};
从'src/app/_services/individual session.service'导入{IndividualSessionService};
从'src/app/_services/navbar.service'导入{NavbarService};
从'src/app/_services/questions.service'导入{QuestionsService};
从'src/app/_services/users.service'导入{UsersService};
@组成部分({
选择器:“正在进行应用程序考试”,
templateUrl:'./正在进行的考试.component.html',
styleUrls:['./正在进行的考试.component.css']
})
导出类组件实现OnInit{
个人会议:个人会议;
用户:用户;
currentUser$:可观察到的;
问题:问题[];;
问题:问题,;
建造商(公共导航服务:导航服务、,
公共个人会话服务:个人会话服务,
公共用户服务:用户服务,
公共会计服务:会计服务,
私人提问服务:提问服务{}
ngOnInit():void{
this.navService.hide();
this.startNewSession();
}
私人startNewSession(){
这是.getCurrentUserData();
if(localStorage.getItem('currentIndividualSession')==null){
此.individualSession={
能力:0.5,
标准错误:1,
startTime:formatDate(新日期(),'yyyy-MM-dd hh:MM:ss','en-us'),
考生编号:1,
会议ID:1
};
setItem('currentIndividualSession',JSON.stringify(this.individualSession));
}
让json=localStorage.getItem('currentIndividualSession');
this.individualSession=JSON.parse(JSON);
这是loadQuestions();
this.currentQuestion=this.questions[0];
}
停止测试(){
this.navService.show();
}
onNextClick(){
this.individualSession.standardError-=0.1;
}
私有getCurrentUserData(){
this.currentUser$=this.accountService.currentUser$;
此.currentUser$.subscribe(currUser=>{
如果(!!用户){
this.loadUser(currUser.email);
}
})
}
loadUser(电子邮件:string){
this.userService.getUser(电子邮件).subscribe(用户=>{
this.user=用户;
})
}
加载问题(){
this.questionService.GetQuestionsFromQuestionly(1).订阅(问题=>{
这个。问题=问题;
});
}
}
你能试试吗

loadQuestions() {
   this.questionService.getQuestionsFromQuestionnaire(1).subscribe(questions => {
      this.questions = questions;
      this.currentQuestion = this.questions[0];
   });
}
并删除
this.currentQuestion=this.questions[0]来自startNewSession()方法。

您能试试吗

loadQuestions() {
   this.questionService.getQuestionsFromQuestionnaire(1).subscribe(questions => {
      this.questions = questions;
      this.currentQuestion = this.questions[0];
   });
}
并删除
this.currentQuestion=this.questions[0]来自startNewSession()方法