我正在动态填充下拉列表及其值。当我单击“提交”按钮后,选择了所有值后,无法在表单中设置其值,我得到的是空白值。
HTML:
<div class="col-md-6" *ngFor="let type of types">
<div class="form-group">
<label>Select {{ type.cat_name }}</label>
<select class="form-control custom-select"
(change)="onChangeType($event.target.value)"
[value]="type.key_name" >
<option value="">-- Select --</option>
<ng-container *ngFor="let subType of dynamicCategoryData">
<option *ngIf="subType.key == type.key_name" [value]="subType.value">{{subType.value}}</option>
</ng-container>
</select>
</div>
</div>
component.ts
ngOnInit() {
this.uploadArtWorkForm = this.formBuilder.group({
thumb_img: ['', [Validators.required]],
categoryArray: this.formBuilder.array([]),
});
}
addUploadArtWorkForm() {
console.log("this.uploadArtWorkForm.value", this.uploadArtWorkForm.value)
}
输出:
{
"categoryArray": [
{
"gems": null,
"metal": null,
"type": null
}
]
}
仅仅是因为我没有使用formControlName
吗?没有它,我们可以做到吗?
任何帮助将不胜感激。
如下修改您的app.component.html:
<div class="col-md-6" formArrayName="categoryArray" *ngIf="uploadArtWorkForm.get('categoryArray')['controls'].length > 0">
<div *ngFor="let item of uploadArtWorkForm.get('categoryArray')['controls']; let k = index">
<div [formGroup]="item">
<div class="form-group ">
<label>Select {{ types[k].cat_name }}</label>
<select class="form-control custom-select" [class]="types[k].key_name" (change)="onChangeType($event.target.value)" formControlName="{{ types[k].key_name }}" *ngIf="types[k].cat_name == 'Type'">
<option value="">-- Select --</option>
<option *ngFor="let type of catTypeValue" [value]="type.cat_name">
{{type.cat_name}}
</option>
</select>
<select class="form-control custom-select" [class]="types[k].key_name" (change)="onChangeType($event.target.value)" formControlName="{{ types[k].key_name }}" *ngIf="types[k].cat_name == 'Metal'">
<option value="">-- Select --</option>
<option *ngFor="let type of catMetalValue" [value]="type.category_id">
{{type.cat_name}}
</option>
</select>
<select class="form-control custom-select" [class]="types[k].key_name" (change)="onChangeType($event.target.value)" formControlName="{{ types[k].key_name }}" *ngIf="types[k].cat_name == 'Gems'">
<option value="">-- Select --</option>
<option *ngFor="let type of catGemsValue" [value]="type.category_id">
{{type.cat_name}}
</option>
</select>
</div>
</div>
</div>
</div>
和以下app.component.ts:
onChangeType(value, id){
console.log(value);
}
onChangeCategory(event, id) {
if (id) {
this.types = [
{"parent_id":1,
"cat_name":"Type",
"category_id":2,
"key_name":"type"},
{"parent_id":1,
"cat_name":"Metal",
"category_id":14,
"key_name":"metal"},
{"parent_id":1,
"cat_name":"Gems",
"category_id":15,
"key_name":"gems"}];
if(this.uploadArtWorkForm.controls['categoryArray'].length > 0){
this.uploadArtWorkForm.controls['categoryArray'].controls.length = 0
}
this.addInNewCategory(this.types);
} else {
this.types = null;
this.subTypes = null;
}
}
get category(): FormArray {
return <FormArray>this.uploadArtWorkForm.controls['categoryArray'];
}
addInNewCategory(types) {['categoryArray'];
types.forEach((element, i) => {
this.category.push(this.formBuilder.group({[element["key_name"]]: new FormControl(element["key_name"])
}));
});
}
}