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?
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));
you can use this to check on form fields per field
this.form.get('firstName').valueChanges.subscribe(data => console.log(data)); 
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With