Checkbox Angular Material Checked By Default
Answer :
You can either set with ngModel either with [checked] attribute. ngModel binded property should be set to 'true':
1.
<mat-checkbox class = "example-margin" [(ngModel)] = "myModel"> <label>Printer </label> </mat-checkbox>
2.
<mat-checkbox [checked]= "myModel" class = "example-margin" > <label>Printer </label> </mat-checkbox>
3.
<mat-checkbox [ngModel]="myModel" class="example-margin"> <label>Printer </label> </mat-checkbox>
DEMO
this works for me in 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
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.
This occurs when you are trying to do bind using the checked property. i.e.
<mat-checkbox [checked]='var' ngModel name='some_name'></mat-checkbox>
And then inside your app.component.ts file
var = true;
will not work.
TLDR: Remove ngModel if you are setting the checked through the [checked] property
<mat-checkbox [checked]='var' name='some_name'></mat-checkbox>
Comments
Post a Comment