简体   繁体   English

Mat-checkbox [checked]="false" 不起作用

[英]Mat-checkbox [checked]=“false” does not work

Trying to implement a checkbox where I can dynamically set the value (and have it appear in the box).尝试实现一个复选框,我可以在其中动态设置值(并使其出现在框中)。

HTML HTML

<mat-checkbox *ngIf="isReply()" class="col-2" formControlName="checkbox" [checked]="false"  >CheckBox off</mat-checkbox> 
<mat-checkbox *ngIf="!isReply()" class="col-2" formControlName="checkbox"  >CheckBox</mat-checkbox> 

materials.module.ts材料模块.ts

addCheckbox() {
      this.checkboxForm = this.fb.group({
        'CheckBox':true,

main.module.ts主模块.ts

isReply(): boolean { 
  return: true;
}

There is a radio button that toggles isReply() off and on.有一个单选按钮可以关闭和打开 isReply()。

When I toggle isReply() on, I can see the CheckBox label change from "CheckBox" to "CheckBox off" but the checkbox status (visibly) does not change.当我打开 isReply() 时,我可以看到 CheckBox 标签从“CheckBox”更改为“CheckBox off”,但复选框状态(可见)没有改变。

I can apply other logic which responds to the checkbox being off, even though the checkbox is still visibly checked (true).我可以应用其他逻辑来响应复选框被关闭,即使复选框仍然被明显选中(真)。 This changes the value of the checkbox to false, even though the checkbox is still visibly checked (true).这会将复选框的值更改为 false,即使复选框仍处于明显选中状态 (true)。

When I click on the checkbox (clear the visible box) the value toggles and the response is as expected, the value of checkbox is now true, even though the box is not checked.当我单击复选框(清除可见框)时,值会切换并且响应符合预期,复选框的值现在为 true,即使未选中该框。


I have made the following changes which STILL do not work我进行了以下更改,但仍然不起作用

adjust this to: 

<div class="row" *ngIf="isReply()">
 <mat-checkbox class="col-2" formControlName="checkBox" >CheckBox</mat-checkbox> 
</div>
<div class="row" *ngIf="!isReply()">
 <mat-checkbox class="col-2" [checked]='true' 
         formControlName="checkBox" >CheckBox</mat-checkbox>
</div>

In the ts:在 ts 中:

addCheckbox() {
      this.checkboxForm = this.fb.group({
        'checkBox':false,

I have two radio buttons (standard & reply).我有两个单选按钮(标准和回复)。

The html for the radio buttons:单选按钮的 html:

<form [formGroup]="materials.SignatureType">
  <mat-radio-group formControlName="sigtype" >Signature Type: &nbsp;
  <label><input type="radio" value="standard" formControlName="sigtype">
  <span>&nbsp;Standard</span>
  </label>
<label><input type="radio" value="reply" (click)="setBoxes()" formControlName="sigtype" >
 <span>&nbsp;Reply</span>
 </label>
</mat-radio-group> 

The code for setBoxes(): setBoxes() 的代码:

if (this.isReply) {
      this.materials.checkboxForm.value.checkBox = false;
    }
    else {
      this.materials.checkboxForm.value.checkBox = true;
    }

The click on "reply" radio button changes the value for the checkBox but does not change the state of the button.单击“回复”单选按钮会更改复选框的值,但不会更改按钮的状态。

I can not get the button state to change OTHER THAN to click on the checkbox.除了单击复选框之外,我无法更改按钮状态。

Using Angular 7.2.3 [(ngModel)] is deprecated.不推荐使用 Angular 7.2.3 [(ngModel)]。

try: In your component.html <div class="container-fluid p-5 mt-2 mb-2" > <mat-checkbox class="col-2" [checked]="var1" >CheckBox off</mat-checkbox> <mat-checkbox class="col-2" [checked]="var2" >CheckBox</mat-checkbox> </div>尝试:在您的 component.html <div class="container-fluid p-5 mt-2 mb-2" > <mat-checkbox class="col-2" [checked]="var1" >CheckBox off</mat-checkbox> <mat-checkbox class="col-2" [checked]="var2" >CheckBox</mat-checkbox> </div>

In your component.ts file var1 = false ;在你的 component.ts 文件中var1 = false ; var2 = true

You just need binding checked property then you can change the value of var1 and var2 in your logic.您只需要绑定检查属性,然后您就可以在逻辑中更改var1var2的值。

I was having this issue with my project.我的项目遇到了这个问题。

I had my checkboxes wired up with the [checked]=... but I had missions getting it to work as expected.我的复选框与[checked]=...连接起来,但我有任务让它按预期工作。 Which was to show non selected items as opacity: 40%;将未选择的项目显示为opacity: 40%; and have the box ticked next to the selected item.并勾选所选项目旁边的框。

eg.例如。 sometimes there would be a checked box and no selected items, or multiple checked boxes, or a selected item and no checked box... Very strange.有时会出现一个选中的框没有选中的项目,或者多个选中的框,或者一个选中的项目没有选中的框……很奇怪。

My conclusion;我的结论; it's being set programatically and being over ridden by the click action.它被编程设置,并通过点击动作正通过缠身。

TLDR; TLDR;

My solution;我的解决方案; change the click action configuration in the module providers by specifying:通过指定以下内容来更改模块提供程序中的单击操作配置:

MAT_CHECKBOX_CLICK_ACTION, useValue: 'noop'

this will leave any click actions in your hands for developer implementation.这将使开发人员实施的任何点击操作都掌握在您手中。

Check the docs for more deets -> https://v6.material.angular.io/components/checkbox/overview检查文档以获取更多 deets -> https://v6.material.angular.io/components/checkbox/overview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM