As I work on creating a data-driven form in Ionic 2 with multiple fields, I encountered an issue while trying to manually update one control's value when the form changes. The error message that keeps appearing is "
TypeError: _this.myForm.controls.name.setValue is not a function
". Despite experimenting with both with and without the <FormControl>
type cast, the problem persists.
import { Component } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, Validators, REACTIVE_FORM_DIRECTIVES } from '@angular/forms';
@Component({
templateUrl: 'build/pages/lead-provider/add-bp/add-bp.html',
directives: [REACTIVE_FORM_DIRECTIVES]
})
export class AddBpPage {
private myForm: FormGroup;
constructor(private formBuilder: FormBuilder) {
this.myForm= formBuilder.group({
'name': ['', [Validators.required]],
'email': ['', [Validators.required]],
'phone': ['', [Validators.required]]
});
this.myForm.valueChanges.subscribe((value) => {
(<FormControl>this.myForm.controls['name']).setValue('abc');
});
}
}