本文介绍了如何在Angular 5中初始化Material Design的mat-slide-toggle?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法以反应形式初始化mat-slide-toggle.
I can't make the initialization of a mat-slide-toggle in a Reactive Form.
模板中有类似内容
<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
])
我也尝试用1或0或false代替true,但是我没有选择默认值.
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.
推荐答案
恕我直言,您的代码存在一些问题.您应该执行以下操作:
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>
在控制器中,您需要分配formControl
,因此,请使用=
而不是:
,即:
And in controller you'll need to assign the formControl
so, use =
instead of :
ie:
X = new FormControl(true, [
Validators.required
]);
这篇关于如何在Angular 5中初始化Material Design的mat-slide-toggle?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!