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

查看:51
本文介绍了如何在角度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天全站免登陆