javascript - Angular Reactive Forms: Dynamic Select dropdown value not binding -
i have 2 arrays of data: associatedprincipals (previously saved data) , referenceprincipals (static data populate in dropdown controls). i'm struggling previous value associatedprincipals displayed/selected in dynamic amount (most examples use single dropdown) of dropdowns on page load.
i'm not how set form (code behind , html), setting select's formcontrolname. currently, static values in each dropdown populate, cannot selected value bind properly.
public ngoninit() { this.factsform = this.formbuilder.group({ associatedprincipals: this.formbuilder.array([]), referenceprincipals: this.formbuilder.array([]) }); // data both of these methods comes external source... var responsedata = // http source... // push retrieved data form this.initprincipals(responsedata[0]); // push static data form this.initstaticdata(responsedata[1]); } public initprincipals(principals?: iassociatedprincipal[]): formarray { principals.foreach((principal) => { this.associatedprincipals.push(this.createprincipalformgroup(principal)); }); } public initstaticdata(response: ireferenceprincipal[]) { response.foreach((principal) => { this.referenceprincipals.push( this.formbuilder.control({ code: principal.code, canhavelead: principal.canhavelead, isduplicate: false })); }); } public createprincipalformgroup(principal: iassociatedprincipal) { return this.formbuilder.group({ code: principal.code, canhavelead: false, isduplicate: false }); } public associatedprincipals(): formarray { return this.factsform.get('associatedprincipals') formarray; } public referenceprincipals(): formarray { return this.factsform.get("referenceprincipals") formarray; }
html:
<form novalidate [formgroup]="factsform"> <div formarrayname="associatedprincipals"> <div *ngfor="let associatedprincipal of associatedprincipals.controls; let i=index;" [formgroupname]="i" > <select class="form-control create-input" formcontrolname="i"> <option value=null disabled selected hidden>--select--</option> <option *ngfor="let refprincipal of referenceprincipals.controls" [ngvalue]="refprincipal">refprincipal.value.code</option> </select> </div> </div> </form>
i appreciate feedback!
edit: added plunker showing issue: https://embed.plnkr.co/xmlvfubuc32estlyldgo/
problems in demo
based on demo provided, there several problems listed below:
- there no
formcontrolname
assignedselect
. - you binding object select's option.
for first problem
since looping through associatedprincipals
show dropdownlist dynamically. , associatedprincipals
formarray can consider below:
associatedprincipals = { "0": formcontrol, "1": formcontrol }
so can assign i
defined @ *ngfor
expression formcontrolname
.
<select formcontrolname="{{i}}" style="margin-top: 10px"> ... </select>
for second problem
while binding object option
, angular compare default value , option
's value object instance default.
you can set same instance(get value of referenceprincipals
's formcontrols) formcontrol of associatedprincipals
(as @fetra r.'s answer). not convenient way since have take logic keep same instance of object.
here give solution using comparewith
directive designed current situation, see docs.
using comparewith
directive, need implement comparefun
tell angular how consider 2 objects(with different instances) same.here yo can include comparing object instance
, comparing object fields
@ same time.
<select formcontrolname="{{i}}" style="margin-top: 10px" [comparewith]="comparefun"> <option value=null disabled selected hidden>--select--</option> <option *ngfor="let refprincipal of referenceprincipals.controls" [ngvalue]="refprincipal.value">{{ refprincipal.value.code }}</option> </select> // tell angular how compare 2 objects comparefn(item1, item2): boolean { return item1 && item2 ? item1.code === item2.code : item1 === item2; }
Comments
Post a Comment