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

时间:2018-01-31 01:46:09

标签: html angular typescript form-control

我正在尝试使用[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>

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

3 个答案:

答案 0 :(得分:6)

由于您拥有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>

答案 1 :(得分:3)

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

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

在您的组件内:

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

或通过HTML:

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

答案 2 :(得分:0)

您需要在app.module.ts内的Imports项下导入FormsModule从{@ angular / forms'导入{FormsModule,ReactiveFormsModule};

@NgModule({
imports: [
     FormsModule      
])}