我有一个这样的对象列表:
listSubject = [
{name: 'Math', lessonPerWeeks: 1},
{name: 'Lit', lessonPerWeeks: 2},
{name: 'Bio', lessonPerWeeks: 3},
{name: 'Phy', lessonPerWeeks: 7},
{name: 'Remains', lessonPerWeeks: 36},
];
我将它们放在一个输入循环中以更改如下值:
<div *ngFor="let subject of listSubject">
<input *ngIf="subject.name != 'Remains'" type="number" [(ngModel)]="subject.lessonsPerWeeks" [formControl]="changeValue">
<div *ngIf="subject.name === 'Remain'">
{{subject.lessonPerWeeks}}
</div>
</div>
FormControl:changeValue 用于检测输入的变化,以重新计算每个主题占总数的百分比:
this.changeValue
.valueChanges
.debounceTime(200)
.subscribe(()=>{
for(let i=0; i < this.listSubject.length;i++){
let subject = this.listSubject[i];
let lastSubject = this.listSubject[this.listSubject.length-1];
subject.percentSem1 = subject.lessonPerWeeks/ this.totalLessonPerWeeks * 100;
if(i === this.listSubject.length - 1){
return;
}
else if(i === 0){
lastSubject.lessonPerWeeks = 36;
}
lastSubject.lessonPerWeeks -= subject.lessonPerWeeks;
}
});
除了输入中的初始显示值外,一切正常。所有输入都显示列表中最后一项的值。在这种情况下,所有输入的值为 7 而不是不同的值。如果我删除 [formControl] ,所有数据都显示完全这是截图:多输入显示
我认为我的问题是对所有输入使用 FormControl。请帮我解决这个问题。谢谢