Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.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
Javascript 索引增量函数绑定到';t递增索引,不遍历数组_Javascript_Arrays_Angular_Loops_Ngfor - Fatal编程技术网

Javascript 索引增量函数绑定到';t递增索引,不遍历数组

Javascript 索引增量函数绑定到';t递增索引,不遍历数组,javascript,arrays,angular,loops,ngfor,Javascript,Arrays,Angular,Loops,Ngfor,Index incremental函数绑定到Angular中的按钮不会增加索引,也不会在单击按钮时遍历数组。无论我不断点击按钮多少次,我都会从数组中看到相同的第一个[0]问题。为什么会这样 component.html 我怎样才能正确点击按钮,反复浏览一系列问题和答案 同样在我的单选按钮中,值工作正常,我将所有三个答案选项绑定到[value],但是单选按钮旁边没有单词,这就像我的{{answer}}被忽略一样。不过,没有错误。谢谢你的帮助。试试看。每次Angular的更改检测启动时,getter

Index incremental函数绑定到Angular中的按钮不会增加索引,也不会在单击按钮时遍历数组。无论我不断点击按钮多少次,我都会从数组中看到相同的第一个
[0]
问题。为什么会这样

component.html

我怎样才能正确点击按钮,反复浏览一系列问题和答案


同样在我的单选按钮中,值工作正常,我将所有三个答案选项绑定到
[value]
,但是单选按钮旁边没有单词,这就像我的
{{answer}}
被忽略一样。不过,没有错误。谢谢你的帮助。

试试看。每次Angular的更改检测启动时,getter都会执行,并为页面重新设置数据

export class QuostionnaireComponent implements OnInit {

  get questions(): any{
    return this.questionnaire.theQuestion[this.incremental];
  }

  get answers(): any{
    return this.questionnaire.theChoices[this.incremental];
  }      
  correstAnswers:any;
  incremental: number = 0;
  constructor(private questionnaire: QuestionnaireService) { }
   nextQuestion():void {
     this.incremental++;
   }

  ngOnInit() {

  }
}

您的问题是您从未从服务中增加到下一组问题/答案。
在您的
nextQuestion
中,您需要将
此。问题
此。答案
重新分配到下一个集合,以便您的组件可以使用新值重新渲染。

不详细介绍您的代码,但构造函数初始化一次,以便始终保持与您的情况相同的值,即
[0]
。您可能想将构造函数代码移动到
ngOnInit
,然后可能会在
ngOnChanges
@Rikin中重新分配问题/答案谢谢,我不知道。但这仍然无济于事。在
ngochanges
的文档中,当指令的任何数据绑定属性发生更改时,它表示调用了
。我不使用指令。在我的component.ts
this.questions=this.question.theQuestion[this.incremental]中只有这个nextQuestion
中,您需要将
this.questions
this.answers
重新分配到下一组,以便您的组件可以使用新值重新渲染。@Rikin谢谢,它可以工作。而且不需要改变,我认为它有不同的用途。你应该把这个作为回答,我会接受的。你知道单选按钮旁边没有文字吗?它可以绑定值,我可以在控制台中看到,但在浏览器中,只有3个单选按钮彼此相邻。是的,我想这是一个比较高级的主题。我从Angular 1中就知道了,但如果仔细看标记,您可以知道ngFor是在HTML上运行的,在您的情况下,它恰好是自关闭的输入,因此在它之外,内部范围值不可用,您试图从中获取
答案
。希望这能澄清问题。
export class QuostionnaireComponent implements OnInit {
  questions:any;
  answers:any;
  correstAnswers:any;
  incremental:Observable = 0;
  constructor(private questionnaire: QuestionnaireService) {
        this.questions = this.questionnaire.theQuestion[this.incremental];
        this.answers = this.questionnaire.theChoices[this.incremental];
   }
   nextQuestion():void {
     this.incremental++;
   }

  ngOnInit() {

  }
}
export class QuostionnaireComponent implements OnInit {

  get questions(): any{
    return this.questionnaire.theQuestion[this.incremental];
  }

  get answers(): any{
    return this.questionnaire.theChoices[this.incremental];
  }      
  correstAnswers:any;
  incremental: number = 0;
  constructor(private questionnaire: QuestionnaireService) { }
   nextQuestion():void {
     this.incremental++;
   }

  ngOnInit() {

  }
}