forms - Angular2 - Manually Set Value for FormBuilder Control -
this driving me nuts, i'm under gun , can't afford spend whole day on this.
i trying manually set control value ('dept') within component, , not working - the new value logs console properly.
here formbuilder instance:
initform() { this.form = this.fb.group({ 'name': ['', validators.required], 'dept': ['', validators.required], 'description': ['', validators.required], }); }
this event handler receives selected dept:
deptselected(selected: { id: string; text: string }) { console.log(selected) // shows proper selection! // how trying set value this.form.controls['dept'].value = selected.id; }
now when form submitted , log out this.form
field still blank! i've seen other ppl use updatevalue()
beta.1 , don't see valid method call on control.
i have tried call updatevalueandvalidity()
no success :(.
i use ngcontrol="dept"
on form element, i'm doing rest of form custom directive/component.
<ng-select [data]="dept" [multiple]="false" [items]="depts" (selected)="deptselected($event)" <!-- how value gets me --> [placeholder]="'no dept selected'"></ng-select>
updated: 19/03/2017
this.form.controls['dept'].setvalue(selected.id);
old:
for forced type cast:
(<control>this.form.controls['dept']).updatevalue(selected.id)
not elegant agree. hope gets improved in future versions.
Comments
Post a Comment