Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2: Subscribe valuechanges in formbuilder

I'm using ionic2 with angular2 and I have a form built with formbuilder but I want to detect new changes of a certain input.

ionViewDidLoad() {
  this.purchaseDataForm = this.formBuilder.group({
    kms: ['', Validators.required],
    lts: ['', Validators.required],
    price: ['', Validators.required],
    total: ['', Validators.required]
  });
}

So that's how my form is validated but I want to subscribe the valuechanges of the price and the lts inputs, do you know how to do that?

I'm trying with the following function but it displays Cannot read property 'valueChanges' of undefined

 ngOnInit() {
  this.purchaseDataForm.price.valueChanges.subscribe(value => {
    console.log(value);
  });
}

Thanks!

like image 271
SoldierCorp Avatar asked Dec 23 '22 23:12

SoldierCorp


1 Answers

The price is not a direct attribute of your form. The price is located in the "controls"-object of your form. So your code should look like this:

ngOnInit() {
    this.purchaseDataForm.controls.price.valueChanges.subscribe(value => {
        console.log(value);
    });
}
like image 122
lastWhisper Avatar answered Dec 28 '22 11:12

lastWhisper