我在角度8中有编辑页面。在此页面中,我有一个formArray,我需要填写它。
我使用formArray设置值形式。
现在我将此代码用作设置值:
ts:
createForm(): void {
this.editQuestionFG = new FormGroup({
title: new FormControl(this.questions.title, Validators.required),
courseExamId: new FormControl(this.questions.courseExamId, Validators.required),
courseOptions: this._formBuilder.array([])
});
}
createItem(): FormGroup {
return this._formBuilder.group({
optionTitle: new FormControl('', Validators.required),
isCorrect: new FormControl(true, Validators.required)
});
}
setItem(item: Options[]) {
const formArray = new FormArray([]);
for (let x of item) {
formArray.push(this._formBuilder.group({
isCorrect: x.isCorrect,
optionTitle: x.optionTitle
}));
}
this.editQuestionFG.setControl('courseOptions', formArray);
console.log(this.editQuestionFG.controls['courseOptions'].value)
}
这是html代码:
<div class="answer col-lg-12 kt-margin-bottom-20-mobile">
<div formArrayName="courseOptions" class="row m-auto"
*ngFor="let project of editQuestionFG.get('courseOptions').controls; let i = index">
<ng-container [formGroupName]="i">
<div class="col-lg-1 kt-margin-bottom-20-mobile icon remove text-center">
<label (click)="deleteItem(i)"><i class="la la-trash"></i></label>
</div>
<div class="col-lg-8 kt-margin-bottom-20-mobile">
<mat-form-field class="mat-form-field-fluid" appearance="outline">
<mat-label>{{'GENERAL.TITLE' | translate}} *</mat-label>
<input matInput formControlName="optionTitle"
[placeholder]="'GENERAL.TITLE' | translate">
</mat-form-field>
</div>
<div class="col-lg-3 kt-margin-bottom-20-mobile icon">
<mat-radio-group name="radp" aria-label="Select an option"
formControlName="isCorrect">
<mat-radio-button value='true'>صحیح</mat-radio-button>
</mat-radio-group>
</div>
</ng-container>
</div>
<div class="add-Item">
<button (click)="AddItems()" mat-raised-button type="button"
color="accent">{{'COURSE_QUESTION.ADD_NEW_OPTION' |translate}}</button>
</div>
</div>
当我使用此代码时,它会填充optionTitle
,但不会在单选按钮值中设置isCorrect
。
是什么问题?我该如何解决??