简体   繁体   中英

Angular: unable to bind select option

I want to bind option values of select box.

<select class="form-control" required name="status" id="status" [ngModel]="status" #status="ngModel">
    <option value="ONE"  [ngValue]="status">Active</option>
    <option value="ZERO"  [ngValue]="status">Deactive</option>
</select>

The values what I received from the API is below:

{id: 1, genreName: "Action", genreDescription: "", status: "ZERO"}

Also, when I submitted the form, I see the console log:

{genre_name: "Action", status: NgModel}

Hi you can check the below link:

https://stackblitz.com/edit/angular-select-dropdown-ufit9g?file=src%2Fapp%2Fapp.component.ts

It has a working solution for it.

Try this:

<select class="form-control" required name="status" id="status" 
    [ngModel]="data.status" (ngModelChange)="onChange($event)" >
    <option [value]="'ONE'"  >Active</option>
    <option [value]="'ZERO'"  >Deactive</option>
</select>

TS:

 data = {id: 1, genreName: "Action", genreDescription: "", status: "ZERO"}
 onChange(e){
   console.log(e)

 }

https://stackblitz.com/edit/angular-wgdhjv?file=src/app/app.component.ts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM