首页 > 解决方案 > 表单重置事件通知?

问题描述

有没有办法在通过 formReset 方法重置表单时得到通知?

我有一个注入表单的指令,当表单被提交或通过重置按钮重置时我可以得到通知,但我无法找到在 ngForm 上调用 formRest 时得到通知的方法。

@Directive({
  selector: '[appForm]'
})
export class FormDirective implements OnDestroy {
  private subscription: Subscription;

  constructor(form: NgForm) {
    this.subscription = form.ngSubmit.subscribe(() => {
      console.log('submitted');
    });
    form.onReset = () => {
      console.log('reset');
    };
  }

  ngOnDestroy() {
    if (this.subscription) {
      this.subscription.unsubscribe();
    }
  }
}

使用类似的指令

<form appForm #form="ngForm">
  <button type="submit">Submit</button>
  <button type="reset">Reset</button>
  <button type="button" (click)="form.resetForm()">Angular reset</button>
</form>

有没有办法通知我的指令 resetForm 方法已被调用?

StackBlitz 的演示https://stackblitz.com/edit/angular-adymlf?file=src/app/form.directive.ts

标签: angularangular-template-form

解决方案



推荐阅读