我已经创建了一个表单,默认情况下,我希望mat-slide-toggle具有“已检查”状态,并尝试了不同的方式,但它始终显示为“ checked = false”状态。
使用: 角度7 角材料7.2
我的表单:
<mat-slide-toggle color="primary" name="activo" formControlName="activo">Activo</mat-slide-toggle>
this.formCrud = new FormGroup({
description: new FormControl(
this.formulario.description, {
validators: [
Validators.required,
Validators.minLength(6),
Validators.pattern('^(?=\\S)[a-zA-Z0-9_\\s-]*\\S$'),
Validators.maxLength(45)
]
}),
activo: new FormControl(this.formulario.activo)
});
1。尝试:
this.formulario.activo = true
2次尝试
<mat-slide-toggle color="primary" name="activo" formControlName="activo" [checked]="formulario.activo">Activo</mat-slide-toggle>
3次尝试
<mat-slide-toggle color="primary" name="activo" formControlName="activo" #toggle>Activo</mat-slide-toggle>
@ViewChild('toggle') matSlideToggle: MatSlideToggle;
ngOnInit() {
this.matSlideToggle.checked = true;
}
4次尝试
<mat-slide-toggle color="primary" name="activo" formControlName="activo" #toggle>Activo</mat-slide-toggle>
@ViewChild('toggle') matSlideToggle: MatSlideToggle;
ngOnInit() {
this.matSlideToggle.toggle();
}
5次尝试
this.formCrud.controls['activo'].setValue(true);