[英]TypeError cannot read property “length” of undefined - angular 4
[英]TypeError: Cannot read property 'length' of undefined in Angular app
我正在創建一個練習練習應用程序,並且需要一次顯示一個MCQ。 編碼了以下內容,但遇到標題中所述的錯誤。
通過在Stack Overflow上搜索其他類似的問題,我確實嘗試了他們的解決方案:嘗試了控制台日志記錄,Node_Modules / typescript中是否缺少任何文件,但是一切都很完美。 還嘗試將Node_Modules和CLI更新到最新版本,但是沒有運氣。
Quiz.component.ts:
export class QuizComponent implements OnInit {
radio1: string;
value: string;
target: any;
quizName = 'QUIZ APPLICATION';
counter=0;
questArray: any[];
questArray1:any[];
questArray2: any[];
constructor(private newService: MyDataService) //service define
{ }
ngOnInit() {
this.newService.fetchData()
.subscribe(response => this.questArray = response.json());
` `
this.callingArray();
}
callingArray() {
for(var i=0;i<=this.questArray.length;i++)
{
this.questArray1=this.questArray[i];
this.questArray2=this.questArray[i+1];
}
}
Service.ts:
export class MyDataService {
constructor(private http: Http)
{}
fetchData()
{
return this.http
.get('../assets/questions.json');
}
}
我真的沒有得到錯過的東西!
JSON:
[
{
"QUES":"The Government of India (GoI) has launched the web-based application e-FRRO scheme for foreigners. What does ‘FRRO’ stands for ?",
"OP":[ "Foreigners Regional Registration Office", "Foreigners Registry Registration Office", "Foreigners Reacceptance Registration Office", "Foreigners Regaining Registration Office" ]
},
{
"QUES": "H",
"OP":["ADASD" , "ASDAD", "ASDASD", "ASDADS"]
}
]
HTML:
<div *ngFor="let abc of questArray1;let j = index" >
{{j+1}}.{{abc.QUES}}
<br>
<input type="radio" name="s">{{abc.OP[j]}}
<br>
<input type="radio" name="s">{{abc.OP[j]}}
<br>
<input type="radio" name="s">{{abc.OP[j]}}
<br>
<input type="radio" name="s">{{abc.OP[j]}}
<br>
</div>
數組迭代發生在您從可觀察到的數據中獲取之前。 嘗試將代碼修改為如下形式:
this.newService.fetchData().subscribe(response => {
this.questArray = response.json();
this.callingArray();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.