0
當沒有選定值時,我一直在努力驗證具有默認「選擇」選項的無效表單。如果表單中存在一個值,那麼我需要選擇相應的值。無效表單驗證將選擇輸入框的默認「選擇」值視爲有效
我使用的反應形式和下面是我的代碼實現:
<select class="form-control" formControlName="environment" >
<option disabled [value]="null">-- Select --</option>
<option *ngFor="let ami of envs" [value]="ami.amiid">
{{ami.name}}
</option>
</select>
this.onlineTestForm = this._fb.group({
.
.
environment: ['', Validators.required],
.
.
});
//value is present in the model
(<FormControl>this.onlineTestForm.controls['environment'])
.setValue(this.model.environment, { onlySelf: true });
//value is not present in model
(<FormControl>this.onlineTestForm.controls['environment'])
.setValue('null', { onlySelf: true });
有沒有更好的方式來實現上述?
也許這將幫助,https://stackoverflow.com/questions/39724161/angular- 2-set-default-value-to-select-option?rq = 1 – arodjabel
我的回答是怎麼回事?它是否適合您的需求或...? :) – Alex