Search code examples
angulartypescriptangular2-forms

Angular2: Detect form changes


In Angular2 4.0 I have a FormGroup looking like this:

this.form = this._fb.group({
      a: ['', [Validators.required]],
      b: ['', [Validators.required]],
      c: ['', [Validators.required]],
    });

Is it possible to subscribe valueChanged on individual fields?

I dont want to detect changes on input c, so the below approach does not work:

this.form.valueChanges.debounceTime(400).subscribe(data => {
     // Do something
    });

Solution

  • You can add separate subscriptions for FormControl a and b.

    this.heroForm.controls["a"].valueChanges.subscribe(data => {
     // Do something
    });
    
    this.heroForm.controls["b"].valueChanges.subscribe(data => {
     // Do something
    });