简体   繁体   English

如何在表单控件中设置默认 boolean false

[英]How to set default boolean false in formcontrol

set default boolean false in formcontrol (primary field) ko在表单控制(主字段)中设置默认 boolean false

this.contactForm = new FormGroup({
      name: new FormControl('', [ Validators.required]),
      contact: new FormControl('', [ Validators.required,Validators.minLength(10)]),
      email: new FormControl('', [ Validators.required,Validators.email]),
      primary: new FormControl('',[])
    });

HTML File:- please see html file HTML 文件:- 请参阅 html 文件

<form [formGroup]="contactForm">
    <mat-checkbox class="example-margin" formControlName="primary" [value]="primary" [(ngModel)]="primary"> Make this primary </mat-checkbox>
  </form>

控制台打印 img

You can use patchValue() in your OnInit() function:您可以在 OnInit() function 中使用patchValue()

this.contactForm.patchValue({primary: false});

First argument of FormControl gets initial value, so you can easily write: FormControl 的第一个参数获取初始值,因此您可以轻松编写:

new FormControl(false, [...]);

Or it could be more complex if you need:或者,如果您需要,它可能会更复杂:

new FormControl({ value: false, disabled: true }, [...]);

You could try this: { email: new FormControl({ value: '', disabled: true }, ...) }你可以试试这个: { email: new FormControl({ value: '', disabled: true }, ...) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM