因此,我有一个添加/编辑屏幕,我需要提交一个列表。(在其他数据中)用户将需要检查这个特定数据的2-3复选框,并且保存的记录将有多个选项映射。
html看起来是这样的。
<div class="col-sm-6 form-group">
<label>P</label>
<div class="form-control details">
<div class="row" formArrayName="pay" *ngFor="let item of payArray.controls; let i = index;">
<div class="col-12" [formGroupName]="i">
<input type="checkbox" formControlName="selected" class="custom-checkbox-input">
<span class="m-l-5" title="{{ item.Id }}">{{item.title}}xy</span>
</div>
</div>
</div>
</div>
在.ts方面,我称之为onInit:
getPay() {
this.PayDS.getItems(true).subscribe(result => {
this.ddlPay = result.list || [];
this.payArray = this.form.get('pay') as FormArray;
this.pay.clear();
this.ddlPay.forEach(item => {
console.log('item.Id = ' + item.Id + ', item.title = ' + item.title);
this.payArray.push(this.formBuilder.group({
Id: new FormControl(item.Id),
title: new FormControl(item.title),
selected: new FormControl({ value: item.selected })
}));
});
});
}
Console.log()正在按需要播放所有记录,Id和标题也是如此。看起来很完美。
在屏幕上,我有相同数量的记录(11),都带有复选框。
这个问题?所有记录都是空的,没有文本,也没有标题,只有11个复选框没有任何文本。
我试过的是:
的任何问题。
<span class="m-l-5" title=""> xy</span>
中元素选项卡中的样子
。
为什么我什么都没看到?我做错了什么?为什么控制台或任何地方都没有错误?我还能做什么呢?
发布于 2022-04-14 14:36:20
<span class="m-l-5">{{item.controls.title.value}}</span>
发布于 2022-04-14 14:18:43
创建一个具有窗体控件(即Id
和title
)的窗体组。
这些是窗体控件,而不是值。
这意味着,要显示它们,您需要这样做:
<span class="m-l-5" title="{{ item.Id.value }}">{{item.title.value}}</span>
由于您没有在selected
中提供formBuilder.group
控件,所以无法绑定到它。试着提供它,使它发挥作用。
而且,它应该是
<div class="col-12" [formGroup]="payArray.controls[i]">
发布于 2022-04-14 14:20:03
您的.subscribe()
不触发更改检测,因此必须手动执行。
将private cd:ChangeDetectorRef
添加到constructor
中,并在forEach
()循环之后添加this.cd.markForCheck();
getPay() {
this.PayDS.getItems(true).subscribe(result => {
...
this.ddlPay.forEach(item => {
...
});
this.cd.markForCheck();
});
}
https://stackoverflow.com/questions/71873062
复制相似问题