简体   繁体   English

默认情况下选中复选框角材料

[英]Checkbox angular material checked by default

I am trying to use an Angular Material checkbox, and set it by default as checked, but it is displayed as non-checked, what is wrong?我正在尝试使用 Angular Material 复选框,并将其默认设置为选中,但显示为未选中,有什么问题?

<mat-checkbox class = "example-margin" [(ngModel)] = obj.impresora> 
     <label>Printer</label> 
</mat-checkbox>

obj.impresora property is boolean obj.impresora 属性是布尔值

You can either set with ngModel either with [checked] attribute.您可以使用 ngModel 或 [checked] 属性进行设置。 ngModel binded property should be set to 'true': ngModel 绑定属性应设置为“true”:

1. 1.

  <mat-checkbox class = "example-margin" [(ngModel)] = "myModel"> 
    <label>Printer </label> 
  </mat-checkbox>

2. 2.

<mat-checkbox [checked]= "myModel" class = "example-margin" > 
    <label>Printer </label> 
</mat-checkbox>

3. 3.

<mat-checkbox [ngModel]="myModel" class="example-margin">
    <label>Printer </label> 
</mat-checkbox>

DEMO演示

this works for me in Angular 7这在 Angular 7 中对我有用

// in component.ts
checked: boolean = true;

changeValue(value) {
    this.checked = !value;
}

// in component.html
<mat-checkbox value="checked" (click)="changeValue(checked)" color="primary">
   some Label
</mat-checkbox>

I hope help someone ... greetings.我希望帮助某人...问候。 let me know if someone have some easiest如果有人有一些最简单的,请告诉我

There are several ways you can achieve this based on the approach you take.根据您采用的方法,有多种方法可以实现这一点。 For reactive approach, you can pass the default value to the constructor of the FormControl(import from @angular/forms)对于反应式方法,您可以将默认值传递给 FormControl 的构造函数(从 @angular/forms 导入)

this.randomForm = new FormGroup({
      'amateur': new FormControl(false),
});

Instead of true or false value, yes you can send variable name as well like FormControl(this.booleanVariable)是的,您可以像FormControl(this.booleanVariable)一样发送变量名,而不是 true 或 false 值

In template driven approach you can use 1 way binding [ngModel]="this.booleanVariable" or 2 way binding [(ngModel)]="this.booleanVariable" like this在模板驱动的方法中,您可以像这样使用 1 路绑定[ngModel]="this.booleanVariable"或 2 路绑定[(ngModel)]="this.booleanVariable"

<mat-checkbox
     name="controlName"
     [(ngModel)]="booleanVariable">
     {{col.title}}
</mat-checkbox>

You can also use the checked directive provided by angular material and bind in similar manner您还可以使用 angular material 提供的 checked 指令并以类似方式绑定

The chosen answer does work however I wanted to make a comment that having 'ngModel' on the html tag causes the checkbox checked to not be set to true.选择的答案确实有效,但是我想发表评论说在 html 标签上有 'ngModel' 会导致选中的复选框未设置为 true。

This occurs when you are trying to do bind using the checked property.当您尝试使用 checked 属性进行绑定时会发生这种情况。 ie IE

<mat-checkbox [checked]='var' ngModel name='some_name'></mat-checkbox>

And then inside your app.component.ts file然后在你的 app.component.ts 文件中

var = true;

will not work.不管用。

TLDR: Remove ngModel if you are setting the checked through the [checked] property TLDR:如果您通过 [checked] 属性设置检查,请删除 ngModel

    <mat-checkbox [checked]='var' name='some_name'></mat-checkbox>

You need to make sure the checked property to be true inside the component.ts您需要确保 component.ts 中的checked属性为真

export class CheckboxComponent implements OnInit {
 checked = true;
}

Make sure you have this code on you component:确保您的组件上有此代码:

export class Component {
  checked = true;
}

If you are using Reactive form you can set it to default like this:如果您使用的是反应式表单,您可以将其设置为默认值,如下所示:

In the form model, set the value to false.在表单模型中,将值设置为 false。 So if it's checked its value will be true else false所以如果它被检查它的值将是真否则假

let form = this.formBuilder.group({
    is_known: [false]
})

//In HTML //在HTML中

 <mat-checkbox matInput formControlName="is_known">Known</mat-checkbox>

Set this in HTML:在 HTML 中设置:

    <div class="modal-body " [formGroup]="Form">
        <div class="">
            <mat-checkbox formControlName="a" [disabled]="true"> Display 1</mat-checkbox>
        </div>
        <div class="">
            <mat-checkbox formControlName="b"  [disabled]="true">  Display 2 </mat-checkbox>
        </div>
        <div class="">
            <mat-checkbox formControlName="c"  [disabled]="true">  Display 3 </mat-checkbox>
        </div>
        <div class="">
            <mat-checkbox formControlName="d"  [disabled]="true">  Display 4</mat-checkbox>
        </div>
        <div class="">
            <mat-checkbox formControlName="e"  [disabled]="true"> Display 5 </mat-checkbox>
        </div>
    </div>

Changes in Ts file Ts 文件中的更改

this.Form = this.formBuilder.group({
a: false,
b: false,
c: false,
d: false,
e: false,
});

Conditionvalidation in Ur Business logic Ur 业务逻辑中的条件验证

if(true){
this.Form.patch(a: true);
}

For check it with ngModel, make a merge between "ngModel" and "value", Example:要使用 ngModel 进行检查,请在“ngModel”和“value”之间进行合并,例如:

 <mat-checkbox [(ngModel)]="myVariable"  value="1" >Subscribe</mat-checkbox>

Where, myVariable = 1其中,myVariable = 1

Greeting问候

// in component.ts
checked: boolean = true;
indeterminate:boolean = false;
disabled:boolean = false;
label:string;

onCheck() {
    this.label = this.checked?'ON':'OFF';
}

// in component.html`enter code here`
<mat-checkbox class="example-margin" [color]="primary" [(ngModel)]="checked" [(indeterminate)]="indeterminate" [labelPosition]="after" [disabled]="disabled" (change)="onCheck()">
                    {{label}}
                  </mat-checkbox>

The above code should work fine.上面的代码应该可以正常工作。 Mat checkbox allows you to make it checked/unchecked, disabled, set indeterminate state, do some operation onChange of the state etc. Refer API for more details. Mat 复选框允许您使其选中/取消选中、禁用、设置不确定状态、对状态进行一些操作等。请参阅API以获取更多详细信息。

Option 1: Setting the checkbox checked from the template:选项 1:设置从模板中选中的复选框:

<mat-checkbox checked="true"> I accept </mat-checkbox>


Option 2: Setting the value dynamically.选项 2:动态设置值。

In the component.html filecomponent.html文件中

<mat-checkbox [checked]="isChecked"> I accept </mat-checkbox>

inside the component.ts filecomponent.ts 文件中

 isChecked:boolean;
 ngOnInit(): void {
  this.isChecked = true;  
 }

You can use您可以使用

<mat-checkbox [attr.checked] = "myCondition ? 'checked' : null">

if the checked attribute is set to null, it gets removed from the html tag如果checked 属性设置为null,它会从html 标签中删除

or you can use Vega's anwser which should work too (mine is a completion that can be usefull if you don't want to link it with ngModel)或者你可以使用 Vega 的 anwser,它也应该可以工作(如果你不想将它与 ngModel 链接,我的完成是很有用的)

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

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