Checkbox angular material checked by default

前端 未结 10 1532
猫巷女王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: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

    
         {{col.title}}
    
    

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

提交回复
热议问题