Angular 7反应形式无法为选择控件设置默认值

问题描述 投票:0回答:1

我实现了一个模式弹出窗口,其中我使用了带有2个值的简单下拉菜单,分别是English和metric,并且我希望将English作为默认选择。我尝试了可能的解决方案,但没有用。

当我放置普通HTML选择下拉菜单时,它显示的是英语,但使用“反应式”表单控件时,它不会作品。

这是我的实现代码

我的下拉列表变量

vitalDropDown: any = [
{ text: "English", value: 1 },
{ text: "Metric", value: 2 }]

我的反应式表单控件

 this.childrenVitalsForm = this._formbuilder.group({
      Id: 0,
      ChildrenId: this.childrenId,
      CheckupDate: ['', Validators.compose([Validators.required])],
      VitalUnit : ['1',Validators.compose([Validators.required])]
    });

我的HTML代码

<select id="VitalUnit" (change)="VitalUnitChanges($event.target.value)"
   class="form-control" formControlName="VitalUnit">
   <option value="1">
      <ng-container i18n>English</ng-container>
   </option>
   <option value="2">
      <ng-container i18n>Metric</ng-container>
   </option>
</select>

我已经尝试过以下可能的解决方法

  1. setValue方法
  2. patchValue方法
  3. 选择属性

他们都不工作。

谢谢

javascript c# asp.net angular angular-directive
1个回答
0
投票

尝试将值更改为[值]并删除(更改)

<select id="VitalUnit" class="form-control" formControlName="VitalUnit">
   <option [value]="'1'">
      <ng-container i18n>English</ng-container>
   </option>
   <option [value]="'2'">
      <ng-container i18n>Metric</ng-container>
   </option>

并编辑您的反应形式控件

 this.childrenVitalsForm = new FormGroup({
      Id: new FormControl(0, []),
      ChildrenId: new FormControl(this.childrenId, []),
      CheckupDate: new FormControl('', [Validators.required]),
      VitalUnit : new FormControl('1', [Validators.required]),
 });
© www.soinside.com 2019 - 2024. All rights reserved.