Angularjs Typescript/Angular-未定义属性-如何访问数组中的变量
我从调试中了解到,q在next()函数中是未定义的。如何访问阵列中的“id”,以便将值从组件(single.value)推送到本地存储?非常感谢你能提供的任何帮助Angularjs Typescript/Angular-未定义属性-如何访问数组中的变量,angularjs,typescript,ionic2,Angularjs,Typescript,Ionic2,我从调试中了解到,q在next()函数中是未定义的。如何访问阵列中的“id”,以便将值从组件(single.value)推送到本地存储?非常感谢你能提供的任何帮助 <ion-navbar color="primary"> <ion-title>Quiz</ion-title> <ion-buttons start> <button ion-button icon-left (click)="prev()">
<ion-navbar color="primary">
<ion-title>Quiz</ion-title>
<ion-buttons start>
<button ion-button icon-left (click)="prev()"><ion-icon name="arrow-back"></ion-icon> Prev</button>
</ion-buttons>
<ion-buttons end>
<button ion-button icon-right (click)="next(q)">Next<ion-icon name="arrow-forward"></ion-icon></button>
</ion-buttons>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-slides #Quiz>
<ion-slide *ngFor="let q of questions">
{{q.question}}
<range [single]="single"></range>
<progress-bar [progress]="loadProgress"></progress-bar>
</ion-slide>
</ion-slides>
</ion-content>
Typescript file:
export class QuizPage {
public RangeComponent;
public ProgressBar;
single = {
Value: 2
};
questions = [
{id: "programming", question: 'Which programming language do you prefer?'}
constructor(public navCtrl: NavController, public storage: Storage, public rangeComponent: RangeComponent, public progressBar: ProgressBarComponent) {
this.randomizeAnswers(this.questions);
this.setData();
this.getData();
}
randomizeAnswers(questions: any[]): any[] {
for (let i = this.questions.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
let temp = this.questions[i];
this.questions[i] = this.questions[j];
this.questions[j] = temp;
}
return this.questions;
}
setData() {
this.storage.set('QuestionData', this.questions);
}
getData() {
this.storage.get('QuestionData').then((data) => {
console.log(data);
});
}
//slides
@ViewChild('Quiz') Quiz: any;
next(q) {
// console.log('single.Value is next');
console.log(this.single.Value);
this.saveToArray(q);
this.single.Value = 2; // Reset value to middle after every question
this.Quiz.slideNext();
this.progressBar.addProgress();
}
saveToArray(q) {
if (q.id == "programming") {
this.storage.set('programming', this.single.Value);
this.storage.get('programming').then((data2) => {
console.log("data logged.");
})
}
测验
上
下一个
{{q.问题}
类型脚本文件:
导出类QuizPage{
公共部门;
公共酒吧;
单个={
价值:2
};
问题=[
{id:“编程”,问题:“您喜欢哪种编程语言?”
构造函数(公共navCtrl:NavController,公共存储:存储,公共rangeComponent:rangeComponent,公共progressBar:ProgressBarComponent){
这个随机回答(这个问题);
这是setData();
这是getData();
}
随机回答(问题:任意[]):任意[]{
for(设i=this.questions.length-1;i>0;i--){
设j=Math.floor(Math.random()*(i+1));
设temp=this.questions[i];
这个问题[i]=这个问题[j];
这个问题[j]=temp;
}
把这封信还给我。问题;
}
setData(){
this.storage.set('QuestionData',this.questions);
}
getData(){
this.storage.get('QuestionData')。然后((data)=>{
控制台日志(数据);
});
}
//幻灯片
@ViewChild(“测验”)测验:任何;
下一个(q){
//log('single.Value是next');
console.log(this.single.Value);
这是saveToArray(q);
this.single.Value=2;//在每个问题后将值重置为中间值
this.quick.slideNext();
this.progressBar.addProgress();
}
saveToArray(q){
如果(q.id==“编程”){
this.storage.set('programming',this.single.Value);
this.storage.get('programming')。然后((data2)=>{
log(“记录的数据”);
})
}
q
在next()
函数中未定义,因为您在*ngFor
之外使用它
您可以使用this.quick.getActiveIndex()
获取滑块的当前位置,然后直接从问题数组获取id,而不是将q传递到下一个函数:
this.questions[this.Quiz.getActiveIndex()].id // check if the slide index starts at 0 or 1
谢谢你的解释。这很有帮助。谢谢!