Manually Set Value for FormBuilder Control
Updated: 19/03/2017
this.form.controls['dept'].setValue(selected.id);
OLD:
For now we are forced to do a type cast:
(<Control>this.form.controls['dept']).updateValue(selected.id)
Not very elegant I agree. Hope this gets improved in future versions.
In Angular 2 Final (RC5+) there are new APIs for updating form values. The patchValue()
API method supports partial form updates, where we only need to specify some of the fields:
this.form.patchValue({id: selected.id})
There is also the setValue()
API method that needs an object with all the form fields. If there is a field missing, we will get an error.
Aangular 2 final has updated APIs. They have added many methods for this.
To update the form control from controller do this:
this.form.controls['dept'].setValue(selected.id);this.form.controls['dept'].patchValue(selected.id);
No need to reset the errors
References
https://angular.io/docs/ts/latest/api/forms/index/FormControl-class.html
https://toddmotto.com/angular-2-form-controls-patch-value-set-value