Checkbox angular material checked by default

前端 未结 11 2374
傲寒
傲寒 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

    
         {{col.title}}
    
    

    You can also use the checked directive provided by angular material and bind in similar manner

提交回复
热议问题