Manually Set Value for FormBuilder Control
This is driving me nuts, I'm under the gun and can't afford to spend another whole day on this.
I am trying to manually set a control value ('dept') within the component, and it's just not working - even the new value logs to console properly.
Here is the FormBuilder Instance:
initForm() {
this.form = this.fb.group({
'name': ['', Validators.required],
'dept': ['', Validators.required],
'description': ['', Validators.required],
});
}
This is the event handler that receives the selected dept:
deptSelected(selected: { id: string; text: string }) {
console.log(selected) // Shows proper selection!
// This is how I am trying to set the value
this.form.controls['dept'].value = selected.id;
}
Now when the form is submitted and I log out this.form
the field is still blank! I've seen other ppl use updateValue()
but this is beta.1 and I don't see that as a valid method to call on the control.
I have also tried to call updateValueAndValidity()
with no success :(.
I would just use ngControl="dept"
on the form element, like I'm doing with the rest of the form but its a custom directive/component.
<ng-select
[data]="dept"
[multiple]="false"
[items]="depts"
(selected)="deptSelected($event)" <!-- This is how the value gets to me -->
[placeholder]="'No Dept Selected'">
</ng-select>
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
If you are using form control then the simplest way to do this:
this.FormName.get('ControlName').setValue(value);
You can use the following methods to update the value of a reactive form control. Any of the following method will suit for your need.
Methods using setValue()
this.form.get("dept").setValue(selected.id);
this.form.controls["dept"].setValue(selected.id);
Methods using patchValue()
this.form.get("dept").patchValue(selected.id);
this.form.controls['dept'].patchValue(selected.id);
this.form.patchValue({"dept": selected.id});
Last method will loop thorough all the controls in the form so it is not preferred when updating single control
You can use any of the method inside the event handler
deptSelected(selected: { id: string; text: string }) {
// any of the above method can be added here
}
You can update multiple controls in the form group if required using the
this.form.patchValue({"dept": selected.id, "description":"description value"});