Html Angular,使用 [disabled] 禁用提交按钮来检查表单有效性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48533227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 15:06:04  来源:igfitidea点击:

Angular, Disable submit button using [disabled] to check form validity

htmlangulartypescriptform-control

提问by Wira Xie

I'm trying to disable a submit button using [disable]=form.controls[...].invalidcondition checking. The submit button should be disabled if there is an input fields is empty or invalid. But looks like i'm doing it wrong. The button is disabled when i fill some fields and left some fields empty, except for the first input field . When i filled the first input field, the button become enabled (while the other input fields are still empty or invalid).

我正在尝试使用[disable]=form.controls[...].invalid条件检查禁用提交按钮。如果输入字段为空或 ,则应禁用提交按钮invalid。但看起来我做错了。当我填写一些字段并将一些字段留空时,该按钮被禁用,除了第一个输入字段。当我填写第一个输入字段时,按钮变为启用(而其他输入字段仍为空或invalid)。

//component.ts
form01: FormGroup;
public myDatePickerOptions: IMyDpOptions = {
  dateFormat: 'dd-mmm-yyyy',
};

setDate(): void {
  let date = new Date();
  this.form01.patchValue({
    DoB: {
      date: {
        year: date.getFullYear(),
        month: date.getMonth() + 1,
        day: date.getDate()
      }
    }
  });
}

clearDate(): void {
  this.form01.patchValue({
    DoB: null
  });
}

constructor(public builder: FormBuilder) {
  this.form01 = this.builder.group({
    email: [null, Validators.required], //text
    DoB: [null, Validators.required], //radio button
    gender: [null, Validators.required], //date picker
  });
}

results: object = new Object();
functionForm01() {
  this.results = [{
    {
      "email": this.form01.controls.email.value
    },
    {
      "DoB": this.form01.controls.DoB.value
    },
    {
      "gender": this.form01.controls.gender.value
    }
  }]
  console.log(this.result);

  this.form01.reset();
}
<!--component.html-->
<div>
  <form [formGroup]="form01">
    email:<input type="text" name="email" formControlName="email" required/> gender:
    <input type="radio" name="gender" formControlName="gender" value="male"> male<br>
    <input type="radio" name="gender" formControlName="gender" value="female"> female<br> DoB:
    <my-date-picker name="DoB" [options]="myDatePickerOptions" formControlName="DoB" required></my-date-picker>

    <button type="submit" [disabled]="form01.controls['email' || 'DoB' || 'gender'].invalid" (click)="functionForm01()">Click</button>
  </form>
</div>

I'm using Angular typescript and myDatePickerpackage from this link. Can anyone help me please?

我正在使用myDatePicker来自此链接的Angular 打字稿和包。有人可以帮我吗?

回答by Abdul-Razak Adam

Since you have your formGroup object, you can disable the button if form01 is not valid

由于您拥有 formGroup 对象,因此如果 form01 无效,您可以禁用该按钮

<!--component.html-->
    <div>
      <form [formGroup]="form01">
        email:<input type="text" name="email" formControlName="email" required/> gender:
        <input type="radio" name="gender" formControlName="gender" value="male"> male<br>
        <input type="radio" name="gender" formControlName="gender" value="female"> female<br> DoB:
        <my-date-picker name="DoB" [options]="myDatePickerOptions" formControlName="DoB" required></my-date-picker>

    <button type="submit" [disabled]="!form01.valid" (click)="functionForm01()">Click</button>

      </form>
    </div>

回答by moohkooh

You can enable/disable the button by checking the validity of your form:

您可以通过检查表单的有效性来启用/禁用按钮:

<button type="submit" [disabled]="!disableBtn">Click</button>

Inside your component:

在您的组件内:

let disableBtn = false;
this. form01.valueChanges 
            .subscribe((changedObj: any) => {
                 this.disableBtn = this. form01.valid;
            });

Or via HTML:

或通过 HTML:

<button type="submit" [disabled]="!form01.valid" (click)="functionForm01()">Click</button>

回答by Nilesh Sutar

You need to import FormsModule inside the app.module.ts under imports import { FormsModule, ReactiveFormsModule } from '@angular/forms';

您需要在 import { FormsModule, ReactiveFormsModule } from '@angular/forms'; 下的 app.module.ts 中导入 FormsModule

@NgModule({
imports: [
     FormsModule      
])}