当我为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
的值时,我在获取A
和B
的值时没有遇到任何问题。代码如下:
<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
示例数据: