Checkbox angular material checked by default

前端 未结 10 1528
猫巷女王i
猫巷女王i 2021-02-11 19:47

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?



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

    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:11

    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:13

    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>
    
    0 讨论(0)
  • 2021-02-11 20:18

    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:21

    Make sure you have this code on you component:

    export class Component {
      checked = true;
    }
    
    0 讨论(0)
  • 2021-02-11 20:24

    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)
提交回复
热议问题