首页 > 解决方案 > 单击以编辑按钮并单击后保存 - 我该怎么做?

问题描述

如何连接这些按钮,以便在按下 Edit 并执行其功能后变成带有其功能的注册?

我的按钮:

<button mat-button color="primary" class="btn-block" type="button" (click)="enableEdit()">Edit</button>
        <button mat-button color="primary" class="btn-block" type="submit" [disabled]="!registerForm.valid">Register</button>

我的 ts 编辑功能:

enableEdit(){
    this.disableForm = !this.disableForm;
    if(!this.disableForm) this.registerForm.disable();
    else this.registerForm.enable();
  }

提前致谢!

标签: angulartypescript

解决方案


您可以简单地onSubmit通过enableEdit以下方式调用:

  enableEdit() {
    this.disableForm = !this.disableForm;
    if (this.disableForm) {
      this.registerForm.disable();
    } else {
      this.registerForm.enable();
      this.onSubmit(); <-- call onSubmit
    }
  }

为了安全起见并避免提交无效表格,您可以onSubmit先检查一下:

  onSubmit() {
    if (!this.registerForm.valid) {
      console.log('Invalid form');
      return;
    }

演示


推荐阅读