How to initializate a mat-slide-toggle of Material Design in Angular 5?

微笑、不失礼 提交于 2019-12-07 08:27:08

问题


I can't make the initialization of a mat-slide-toggle in a Reactive Form.

I have something like in the template

<mat-slide-toggle name="X" formControlName="X" color="primary"></mat-slide-toggle>

and in the controller I am doing

X:  new FormControl(true, [
      Validators.required
    ])

I have also tried with 1 or 0 or false instead of true but I didn't get it with a default value selected.

I would appreciate if someone could help me, many thanks.


回答1:


IMHO, there's some issue with your code. You should do something like this:

<mat-slide-toggle [formControlName]="X" [checked]="X.value" (click)="X.value =!X.value" color="primary">{{X.value ? 'ON' : 'OFF'}}</mat-slide-toggle>              

And in controller you'll need to assign the formControl so, use = instead of : ie:

 X = new FormControl(true, [
      Validators.required
 ]);


来源:https://stackoverflow.com/questions/50840348/how-to-initializate-a-mat-slide-toggle-of-material-design-in-angular-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!