【问题标题】:Radio buttons are checked by default, which they shouldn't单选按钮默认被选中,它们不应该被选中
【发布时间】:2019-09-30 19:57:40
【问题描述】:

我有一个单选按钮 - 如果它被选中,它应该显示带有选项 1 和选项 2 的 mat-radio-group。mat-radio-group 按预期隐藏 - 但是,默认情况下,在启动我的应用程序时,所有单选按钮都被选中并且我无法取消选中其中任何一个

我显然希望它们在启动时都未选中,这是/应该是默认行为。我做错了什么让他们默认检查?

Dialog.component.html

<mat-radio-button (click)="toggleDisplay('options')" class="item">See options</mat-radio-button>
  <mat-radio-group *ngIf="doShow('options')" class="item-container">
    <mat-radio-button class="item">Option 1</mat-radio-button>
    <mat-radio-button class="item">Option 2</mat-radio-button>
  </mat-radio-group>
</mat-radio-group>

Dialog.component.ts

currentlyShown = '';

toggleDisplay(element: string) {
  this.currentlyShown = element;
}

doShow(element: string): boolean {
  return this.currentlyShown === element;
}

提前致谢。 (已排除所有不必要的代码

【问题讨论】:

  • 请提供 Stackblitz
  • 单选按钮有。如果您不提供,则会对其进行检查。

标签: angular angular-material


【解决方案1】:

单选按钮具有值。如果您不提供,默认情况下会检查它们。

Stackblitz

<mat-radio-group aria-label="Select an option">
  <mat-radio-button value="1">Option 1</mat-radio-button>
  <mat-radio-button value="2">Option 2</mat-radio-button>
  <mat-radio-button>Option 1</mat-radio-button>
  <mat-radio-button>Option 2</mat-radio-button>
</mat-radio-group>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    • 2018-12-26
    • 1970-01-01
    • 2016-06-09
    • 2017-10-02
    • 2021-02-23
    • 2013-10-02
    相关资源
    最近更新 更多