Checkbox angular material checked by default

前端 未结 11 2375
傲寒
傲寒 2021-02-11 19:37

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?



        
相关标签:
11条回答
  • 2021-02-11 20:11

    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

    0 讨论(0)
  • 2021-02-11 20:11

    Set this in 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

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

    Conditionvalidation in Ur Business logic

    if(true){
    this.Form.patch(a: true);
    }
    
    0 讨论(0)
  • 2021-02-11 20:18

    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

    0 讨论(0)
  • 2021-02-11 20:20

    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

    0 讨论(0)
  • 2021-02-11 20:23

    You need to make sure the checked property to be true inside the component.ts

    export class CheckboxComponent implements OnInit {
     checked = true;
    }
    
    0 讨论(0)
  • 2021-02-11 20:27
    // 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.

    0 讨论(0)
提交回复
热议问题