Material 单选按钮内的 Angular Material 表单字段不会触发单选按钮

标签 angular angular-material

当我在 radio button 中有一个 form field 时,单击 form field 时单选按钮没有被选中。 如果我在单选按钮区域内但在表单字段外单击,它确实会被选中。

当我点击表单域时,我可以看到单选按钮的动画被选中但实际上没有被选中。

简化代码:

<mat-radio-button>
  <mat-form-field>
    <input matInput>
  </mat-form-field>
</mat-radio-button>

如何让单选按钮在用户点击字段后被选中?

-编辑-

mat-form-field 是强制性的。或者是否有更好的方法让单选按钮在用户单击该字段后自动选中?

最佳答案

这使用 react 形式。

您可以在 mat-form-field 上使用普通的 (click) 事件。

因为点击事件传播这不会干扰任何东西。

<mat-radio-group formControlName="refundType">

   <mat-radio-button [value]="'partial-refund'" fxLayout="row">

        <mat-form-field appearance="standard" 
                        (click)="selectPartialCheckbox()">

             <mat-label>Partial Refund</mat-label>

             <input matInput [placeholder]="'Amount to refund'" 
                    formControlName="amount"/>

        </mat-form-field>

   </mat-radio-button>

   <!-- other items not shown -->

</mat-radio-group>

您实际设置该值的具体方式取决于您的编码标准,但像这样:

selectPartialCheckbox()
{
    this.form.controls['refundType'].setValue('partial-refund');
}

不要尝试检查 DOM 中的复选框 - 那是在自找麻烦。

关于 Material 单选按钮内的 Angular Material 表单字段不会触发单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51297261/

相关文章:

angular - RxJS 根据先前的数据响应执行请求(涉及对象数组的循环)

javascript - ionic 5 : Is there a way to combine tabs and side menu

webpack - 如何将 bootstrap-material-design 包添加到 webpack 中?

angular-material - 'mat-select' 在 Angular 5 中构建时不是已知元素

html - 在 CSS 中复制 Angular Material 设计字体规范

css - 在 Angular Material 中一起使用 sidenav 和工具栏时,页面不应滚动

Angular 国际化插值字符串

node.js - Mean Stack 安装在端口 80 或 iis 下

node.js - 我不知道为什么我的 http 请求在我的 Angular 项目中不起作用?

javascript - AngularJS ctrl.$isEmpty 返回值而不是 bool 值