typescript Angular 10,默认情况下不选中单选按钮

u3r8eeie  于 2023-03-04  发布在  TypeScript
关注(0)|答案(3)|浏览(190)

我们有单选按钮组,并尝试使默认选择为 * 女性 * 单选选项。

<ng-template #genderTemplate>
      <div>
        <div>
          <label class="ari-label" for="gender">Gender</label>
        </div>
        <label class="ari-control ari-control-radio ari-control-inline">
          <input formControlName="gender" type="radio" [value]="true" /> Male
          <div class="ari-control-indicator"></div>
        </label>
        <label class="ari-control ari-control-radio ari-control-inline">
          <input formControlName="gender" type="radio" [value]="false" [checked]="!gender"/> Female
          <div class="ari-control-indicator"></div>
        </label>
      </div>
    </ng-template>

不管用了。有线索吗?

mfuanj7w

mfuanj7w1#

您将在html中添加name=“gender”,在.ts中添加gender:boolean=false;

<ng-template #genderTemplate>
<div>
  <div>
    <label class="ari-label" for="gender">Gender</label>
  </div>
  <label class="ari-control ari-control-radio ari-control-inline">
    <input formControlName="gender" type="radio" name="gender" ngModel [value]="true" /> Male
    <div class="ari-control-indicator"></div>
  </label>
  <label class="ari-control ari-control-radio ari-control-inline">
    <input formControlName="gender" type="radio" name="gender" ngModel [value]="false" [checked]="!gender"/> Female
    <div class="ari-control-indicator"></div>
  </label>
</div>
pxq42qpu

pxq42qpu2#

问题似乎出在Female单选按钮的[checked]属性上,它被设置为! gender,但是组件代码中没有定义名为gender的变量或属性。
要设置Female单选框选项的默认选择,您可以修改代码,如下所示:

<ng-template #genderTemplate>
  <div>
    <div>
      <label class="ari-label" for="gender">Gender</label>
    </div>
    <label class="ari-control ari-control-radio ari-control-inline">
      <input formControlName="gender" type="radio" [value]="true" /> Male
      <div class="ari-control-indicator"></div>
    </label>
    <label class="ari-control ari-control-radio ari-control-inline">
      <input formControlName="gender" type="radio" [value]="false" [checked]="true"/> Female
      <div class="ari-control-indicator"></div>
    </label>
  </div>
</ng-template>

通过在"女性"单选按钮上设置[checked]="true",默认情况下将选中该选项。
希望这有帮助!

5ssjco0h

5ssjco0h3#

在HTML模板中,我添加了name属性来将单选按钮分组在一起。我将male的值更改为'male',将female的值更改为'female'。然后,我将Typescript中gender formControl的默认值设置为'female',这样,最初将选择female按钮。
最好为性别类型初始化一个枚举。
因此,模板应类似于:

<ng-template #genderTemplate>
  <div>
    <div>
      <label class="ari-label" for="gender">Gender</label>
    </div>
    <label class="ari-control ari-control-radio ari-control-inline">
      <input
        formControlName="gender"
        type="radio"
        name="gender"
        value="male"
      />
      Male
      <div class="ari-control-indicator"></div>
    </label>
    <label class="ari-control ari-control-radio ari-control-inline">
      <input
        formControlName="gender"
        type="radio"
        name="gender"
        value="female"
      />
      Female
      <div class="ari-control-indicator"></div>
    </label>
  </div>
</ng-template>

typescript 窗体组:

form: UntypedFormGroup;
constructor(private fb: UntypedFormBuilder) {
    this.form = this.fb.group({
        gender: ['female'],
        // all the other controls in the form
    });
}

相关问题