ngModel在ngFor循环上没有给出正确的值

时间:2018-01-04 10:23:15

标签: angular typescript ionic-framework ionic2 ionic3

当我为ngFor [(ngModel)]时,结果是错误的。我尝试根据此plunker进行关注。

<div class="answer-field" *ngFor="let answer of data.answer.scheme_group_id; let i=index; trackBy:trackByIndex;">
    <div class="answer-numbering">
        {{answer}}
    </div>
    <input class="answer-input" [(ngModel)]="data.answer.scheme_group_id[i]" name="answer_fields" (input)="onAnswerChange(answer.answer_id,$event.target.value)" type="text" placeholder="Type your answer here" />
</div>

但是,当我硬编码[(ngModel)]=A[(ngModel)]=B的值时,我在获取AB的值时没有遇到任何问题。代码如下:

<div class="answer-field">
    <div class="answer-numbering">
        A
    </div>
    <input class="answer-input" [(ngModel)]="A" name="answer_fields" (input)="onAnswerChange(answer.answer_id,$event.target.value)" type="text" placeholder="Type your answer here" />
</div>  
<div class="answer-field">
    <div class="answer-numbering">
        B
    </div>
    <input class="answer-input" [(ngModel)]="B" name="answer_fields" (input)="onAnswerChange(answer.answer_id,$event.target.value)" type="text" placeholder="Type your answer here" />
</div>

调试代码:

A:any; 
  B:any;
  answer_field:any;
  onAnswerChange(answerValue : string ) {  
      let answer_field_change = true;
      this.event.publish("answer_field_changes",answer_field_change);

      let output_data = {"A":this.A, "B":this.B};
      console.log('output_data');
      console.log(output_data);

  }


 trackByIndex(index: number, obj: any): any {
    return index;
  }

伙计们......请帮助我..我的预期输出是在[(ngModel)]循环中使用ngFor但仍然无效。

我的data.answer.scheme_group_id示例数据:

enter image description here

0 个答案:

没有答案