component.ts
types : any[] = [
{ value : '1'},
{ value : '2'},
{ value : '3'}
];
createForm = this.fb.group({
firstInput:['',Validators.required],
secondInput:['',Validators.required],
thirdInput:['',Validators.required],
});
component.html
<select class="form-control" [value]="selected"[(ngModel)]="selectedValue">
<option *ngFor="let x of types">{{x.value}}</option>
</select>
<div *ngIf="selectedValue == '1'">
<label for="1" class="control-label">1</label>
<input id="1" class="form-control" type="text" formControlName="firstInput">
</div>
<div *ngIf="selectedValue == '2'">
<label for="2" class="control-label">2</label>
<input id="2" class="form-control" type="text" formControlName="secondInput">
</div>
<div *ngIf="selectedValue == '3'">
<label for="3" class="control-label">3</label>
<input id="3" class="form-control" type="text" formControlName="thirdInput">
</div>
所有字段都是必填字段,我的问题是:当我选择“ 1”作为示例而不填写“ 1”的输入时,然后我将第二个选项更改为“ 2”并填写,我无法提交表格因为fielControllName“ firstInput”尽管是不可见的,但仍为空,因此每次更改时我都需要清除ngModel的选定值(按照我的想法)。
答案 0 :(得分:1)
因此,最初,您的表单必须类似于:
foreach (DataGridCell cell in AppraiseeDataGrid.SelectedCells)//It says it cant convert type datagridcellinfo to datagricell
您的HTML代码:
createForm = this.fb.group({
selectedValue:[null,Validators.required],
firstInput:[null],
secondInput:[null],
thirdInput:[null],
});
然后您必须具有更改功能:
<form [formGroup]="createForm">
<select class="form-control" formControlName="selectedValue">
<option *ngFor="let x of types" value="{{x.value}}">{{x.value}}</option>
</select>
<div *ngIf="createForm.value.selectedValue== '1'">
<label for="1" class="control-label">1</label>
<input id="1" class="form-control" type="text" formControlName="firstInput">
</div>
<div *ngIf="createForm.value.selectedValue== '2'">
<label for="2" class="control-label">2</label>
<input id="2" class="form-control" type="text" formControlName="secondInput">
</div>
<div *ngIf="createForm.value.selectedValue== '3'">
<label for="3" class="control-label">3</label>
<input id="3" class="form-control" type="text" formControlName="thirdInput">
</div>
</form>
如果您想将值设为空,则可以使用:
OnChange() {
this.createForm.get('selectedValue').valueChanges.subscribe(
val => {
if (val==1) {
this.createForm.get('firstInput').setValidators([Validators.required]);
this.createForm.controls['firstInput'].updateValueAndValidity();
this.createForm.get('secondInput').setValidators([]);
this.createForm.controls['secondInput'].updateValueAndValidity();
this.createForm.get('thirdInput').setValidators([]);
this.createForm.controls['thirdInput'].updateValueAndValidity();
}
else if (val==2) {
this.createForm.get('firstInput').setValidators([]);
this.createForm.controls['firstInput'].updateValueAndValidity();
this.createForm.get('secondInput').setValidators([Validators.required]);
this.createForm.controls['secondInput'].updateValueAndValidity();
this.createForm.get('thirdInput').setValidators([]);
this.createForm.controls['thirdInput'].updateValueAndValidity();
}
else if (val==3) {
this.createForm.get('firstInput').setValidators([]);
this.createForm.controls['firstInput'].updateValueAndValidity();
this.createForm.get('secondInput').setValidators([]);
this.createForm.controls['secondInput'].updateValueAndValidity();
this.createForm.get('thirdInput').setValidators([Validators.required]);
this.createForm.controls['thirdInput'].updateValueAndValidity();
}
}
)
}