【发布时间】:2019-11-14 19:46:40
【问题描述】:
我有一个简单的动态单选按钮,在我的示例中,3 是通过返回数据创建的。
选择无线电值时,它将我的反应性形式带有该值“是”,“否”或“也许”。在这些对象中,我希望将其他属性作为表单的一部分发回。
如下我返回section,还想返回匹配的section的sectionCode。
在更改单选选择之前,我的表单正确显示了所有属性,但显然我只能传回一个值,所以一旦我这样做,其他对象项就会丢失。
https://stackblitz.com/edit/angular-r4g6uv?file=src%2Fapp%2Fpersonal%2Fpersonal.component.ts
section:[
{
section: "yes",
sectionCode: "1"
},
{
section: "no",
sectionCode: "2"
},
{
section: "maybe",
sectionCode: "3"
}
]
component.html
<div formGroupName="radioButtons" class="form-group col-6 pl-0 pt-3">
<h2 class="mb-2">radioButtons</h2>
<label for="intel-form-submitter" appRequiredLabel>select</label><br />
<div class="form-check-inline" *ngFor="let item of personal.radioButtons.section">
<label for="{{item.section}}" class="col-12 customradio"
><span>{{item.section}}</span>
<input value="{{item.section}}" id="{{item.section}}" type="radio" formControlName="section"/>
<span class="checkmark"></span>
</label>
</div>
</div>
component.ts
createEntry(formBuilder: FormBuilder) {
return this.formBuilder.group({
title: this.personal.title,
creationDate: this.personal.creationDate,
radioButtons: this.formBuilder.group({
section: this.personal.radioButtons.section,
})
});
}
【问题讨论】:
-
您可以通过属性找到对象。
this.section.find(element => element.section == this.personal.radioButtons.section);并使用它作为回报。 -
您是否将其添加到表单生成器条目中?如果你能展示一个很好的例子。
标签: javascript angular typescript angular-reactive-forms