首页 > 解决方案 > Ionic 3 ngFor 离子选项值错误

问题描述

i'm building a crud app with ionic 3, the app is working, but i have a problem with the ion-select, it's supposed to load the categories from the database and it does that, but when select an option throws this error:在此处输入图像描述

在此处输入图像描述


这是html代码:

<ion-select [(ngModel)]="categoria" id="categoria" name="categoria"okText="OK" cancelText="Cancelar">
<ion-option *ngFor="let categ of categoria" [value]="categ.id">{{categ.nombre_categoria }}</ion-option>
</ion-select>


这是功能

getCategoria(){
this.NETP.categoria().then(
data=>{
this.categoria = data;
})
}


我不知道怎么了,我是菜鸟,谢谢建议
控制台中的数据看起来像这样

在此处输入图像描述

标签: angularionic-framework

解决方案


在您的代码中,您将选定的选项绑定到您用来迭代的类别。因此,将您的绑定更改为任何其他值 ex : selectedCategoria 并在您的 ts.xml 中声明它。

html: <-- 将类别更改为 selectedCategoria-->

 <ion-select [(ngModel)]="selectedCategoria" id="categoria" 
  name="categoria"okText="OK" cancelText="Cancelar">
  <ion-option *ngFor="let categ of categoria" [value]="categ.id"> 
     {{categ.nombre_categoria }}
  </ion-option>
</ion-select>

推荐阅读