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?
<mat-checkbox class = "example-margin" [(ngModel)] = obj.impresora>
<label>Printer</label>
</mat-checkbox>
obj.impresora property is boolean
Angular Material can be checked dynamically using checked property of <mat-checkbox> element. Checkbox can also be checked using Angular ngModel . The ngModel can be used for two-way binding.
The ng-checked Directive in AngularJS is used to read the checked or unchecked state of the checkbox or radio button to true or false. If the expression inside the ng-checked attribute returns true then the checkbox/radio button will be checked otherwise it will be unchecked.
Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.
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>
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)
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)
In template driven approach you can use 1 way binding [ngModel]="this.booleanVariable"
or 2 way binding [(ngModel)]="this.booleanVariable"
like this
<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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With