Using two-way-binding and model-driven form is not the intended way, since form control could be considered to replace the ngModel. But it can be done, but using ngModel is then pretty much unnecessary, because despite that, you need in your formcontrol make a reference to your loggedUser
. As you can see from the plunker at the bottom, the object created by the form, has the exact value and build as the loggedUser
, so the form object would then equal the object from the form. Depending on how you build the form, the object of the form could then simply be assigned to loggedUser
upon submit. Also as a comment, the banana in the box, i.e [(ngModel)]
equals the following
[ngModel]="someVariable" (ngModelChange)="someMethod($event)"
So now you are using both the "banana in the box" and ngModelChange
, so I recommend you use either it, or then the one-way-binding and ngModelChange
.
But back to your code and making the reference, it can be made when you build the form:
this.myForm = this.fb.group({
// find the object in the infos array that matched the value of loggeduser
info: [this.infos.find(x => x.id == this.loggedUser.info).id]
})
If this the retrieval of values for loggedUser
is asynchronous, you need to either wait to build the form until value can be set, or you initially build an empty form and patch the values once they have been retrieved.
Hope this helps, and here's a Plunker to play with.