如何在 Angular 5 中初始化 Material Design 的 mat-slide-toggle? [英] How to initializate a mat-slide-toggle of Material Design in Angular 5?

查看:22
本文介绍了如何在 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.

我在模板中有类似的东西

I have something like in the template

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

在我正在做的控制器中

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆