角度:无法绑定选择选项

时间:2020-02-25 11:06:39

标签: angular

我想绑定选择框的选项值。

<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>

我从API收到的值如下:

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

此外,当我提交表单时,我看到了控制台日志:

{genre_name:“动作”,状态:NgModel}

2 个答案:

答案 0 :(得分:0)

您好,您可以检查以下链接:

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

它有一个可行的解决方案。

答案 1 :(得分:0)

尝试一下:

<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