Angular 4 / Reactive form and json as select value -


i have reactive form created in component:

this.details = this.formbuilder.group({   status: {} }); 

then in template have:

<select class="form-control" formcontrolname="status">     <option *ngfor="let status of formvalues?.status" [ngvalue]="status">{{status.name}} {{status.description}}</option> </select> 

and onchanges called:

  this.details.setvalue({     status: this.data.status   }); 

the idea is, have single select, has have object value. can't make status nested formgroup, since have spitted 3 formcontrols. works fine, despite setvalue. select not getting default data. however, when change option in select, form model updates correctly.

any ideas :)?

first off syntax option is: <option *ngfor="let entry of entries" [value]="entry">{{entry}}</option> value instead of ngvalue. secondly u need use 2 way data biding right values emited template model , not other way. add [(ngmodel)] select

<select [(ngmodel)]=status...>

one more thing suggested changing name in form builder don't have clash in ngfor loop status:{} , *ngfor="let status of formvalues?.status

usage example ngmodel , select official docs (link below):

<div class="form-group"> <label for="power">hero power</label> <select class="form-control" id="power" required [(ngmodel)]="model.power" name="power"> <option *ngfor="let pow of powers" [value]="pow">{{pow}}</option> </select> </div>

[1] https://angular.io/guide/forms


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -