如何在角度 4 中实现自动保存反应形式? [英] How to implement auto save a reactive form in angular 4?

查看:25
本文介绍了如何在角度 4 中实现自动保存反应形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在表单有效时自动以反应形式保存内容而无需单击保存按钮.

I want to auto save content in reactive form when form is valid without clicking save button.

推荐答案

您将需要订阅 FormGroup 的 statusChanges() 方法,并且在该 Observable 中您可以确定 FormGroup 是否为有效,然后触发保存事件.

You will want to subscribe to the statusChanges() method of your FormGroup, and in that Observable you can determine whether the FormGroup is valid and then trigger a save event.

import 'rxjs/add/operator/takeWhile';
import { FormBuilder, FormGroup } from '@angular/forms';

@Component({...})
export class MyComponent {

  private form: FormGroup;
  private alive: boolean;

  constructor(private formBuilder: FormBuilder) {}

  public ngOnInit(): void {
    this.alive = true;
    this.form = this.formBuilder.group({
      // your form configuration
    });

    this.form.statusChanges()
      .takeWhile(this.alive) // only subscribe while this component is alive
      .subscribe((status) => {
        // if status is valid, auto-save
      });
  }

  public void ngOnDestroy() {
    this.alive = false;
  }
}

这篇关于如何在角度 4 中实现自动保存反应形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆