【发布时间】:2019-12-11 04:49:33
【问题描述】:
在我的表单中我需要添加公司,我想在数据库中保存我添加的公司的字符串列表,问题是formarray中公司的值始终为空
<button (click)="addNewCompany()">Add new Company</button><br><br>
<form [formGroup]="myForm">
<div formArrayName="companies">
<div *ngFor="let comp of getForm(); let i=index>
<fieldset>
<legend>
<h3>COMPANY {{i+1}}: </h3>
</legend>
<label>Company Name: </label>
<input formControlName="comp" /><span><button (click)="deleteCompany(i)">Delete Company</button></span>
</fieldset>
</div>
</div><br>
</form>
export class AppComponent {
myForm: FormGroup;
constructor(private fb: FormBuilder) {
this.myForm = this.fb.group({
companies: this.fb.array([])
});
}
getForm(): any {
return this.myForm.get("companies")["controls"];
}
addNewCompany() {
const control = <FormArray>this.myForm.get("companies");
control.push(this.fb.control(''));
}
deleteCompany(index) {
let control = <FormArray>this.myForm.controls.companies;
control.removeAt(index);
}
}
【问题讨论】:
-
嗨。您应该将一些相关代码与 StackBlitz 链接一起发布 - 您更有可能以这种方式获得答案????
标签: angular typescript