Javascript 向动态创建的字段添加数据
我使用反应式表单动态添加一些字段,并在选择框中显示数据(数组格式的api响应),到目前为止,我在表单中获得了值,但无法设置为选择框。请纠正我,我是新的反应形式 组件。ts htmlJavascript 向动态创建的字段添加数据,javascript,angular,typescript,Javascript,Angular,Typescript,我使用反应式表单动态添加一些字段,并在选择框中显示数据(数组格式的api响应),到目前为止,我在表单中获得了值,但无法设置为选择框。请纠正我,我是新的反应形式 组件。ts html 属性(例如颜色) {{value}} 值(例如红色、蓝色、绿色) 您需要将属性和所选属性分开。属性用于保存从响应Api初始化的值。SelectedAttribute用于从用户选择获取所选值 ngOnInit() { this.dynamicForm = this.formBuilder.group({
属性(例如颜色)
{{value}}
值(例如红色、蓝色、绿色)
您需要将属性和所选属性分开。属性用于保存从响应Api初始化的值。SelectedAttribute用于从用户选择获取所选值
ngOnInit() {
this.dynamicForm = this.formBuilder.group({
attributes: new FormArray([])
});
}
get f() { return this.dynamicForm.controls; }
get a() { return this.f.attributes as FormArray; }
addAttitubute(){
let array=[]
this.attributeService.getAttributes().subscribe(response =>{
response.forEach(value =>{
array.push(value.name);
})
this.a.push(this.formBuilder.group({
attribute: this.formBuilder.array(array),
value: this.formBuilder.array([])
}));
})
console.log(this.a);
}
<label>
<div *ngFor="let t of a.controls; let i = index" class="field-heading">Attribute (e.g. Colour)
<div [formGroup]="t" >
<select formControlName="attribute" required class="attr-dropdown">
<option *ngFor="let value of attribute">
{{ value }}
</option>
</select>
</div>
<div class="flex-one">
<label>
<div class="field-heading">Value (e.g. Red, Blue, Green)</div>
<p-chips inputStyleClass="full-width theme-input" ></p-chips>
</label>
</div>
</div>
</label>