Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to watch on few fields changes in long form in Angular 2

My form has 10 to 15 fields I just want to watch the changes on 2 fields built using form-builder.

Currently i can watch all the fields in the form using the following code :

  export class App {
  constructor(private formBuilder: FormBuilder) {
    this.form = formBuilder.group({
      firstName: 'John',
      lastName: 'Fleming',
      Name: 'John Fleming',
      Address : '12331 wiley post',
      city: '',
      state:'',
      ...... so on    
    })

    this.form.valueChanges.subscribe(data => {
      console.log('Form changes', data)
      this.output = data
    })
  }
}

I don't want to watch all the fields, I used (keyup) event on those fields to handle it for now. But I want to know whether there is any better way of doing the same in angular2?

like image 874
user1188867 Avatar asked Aug 11 '17 05:08

user1188867


2 Answers

I can't try this right now to confirm the syntax ... but something like this:

import { merge } from 'rxjs';    
merge(
      this.form.get('firstName').valueChanges,
      this.form.get('lastName').valueChanges
    )
     .subscribe(data => console.log(data));
like image 106
DeborahK Avatar answered Nov 12 '22 11:11

DeborahK


you can use this to check on form fields per field

this.form.get('firstName').valueChanges.subscribe(data => console.log(data)); 
like image 20
Rahul Singh Avatar answered Nov 12 '22 11:11

Rahul Singh