我有一个名为 options 的对象数组 .
这是我的HTML代码
<ion-item>
<ion-label>place</ion-label>
<ion-select [(ngModel)]="place" (click)="optionsFn(item);">
<ion-option value="item" *ngFor="let item of options">{{item.name}} {{item.price}}</ion-option>
</ion-select>
</ion-item>
{{salespriceOp}}
{{quantityOp}}
这是我的.ts文件代码
product_option_value_idOp
priceOp
salespriceOp
quantityOp
skuOp
nameOp
options = [
{
"product_option_value_id": "45",
"name": "Bangalore Auto",
"quantity": "12",
"sku": "56876",
"price": "100.00",
"salesprice": "50"
},
{
"product_option_value_id": "51",
"name": "Hyderabad Auto",
"quantity": "23",
"sku": "56543",
"price": "200.00",
"salesprice": "60"
},
{
"product_option_value_id": "52",
"name": "Delhi Auto",
"quantity": "14",
"sku": "98767",
"price": "300.00",
"salesprice": "80"
}
];
constructor(public navCtrl: NavController) {
}
optionsFn(item) {//here item is an object
console.log(item);
this.product_option_value_idOp = item.product_option_value_id;
this.priceOp = item.price;
this.salespriceOp = item.salesprice;
this.quantityOp = item.quantity;
this.skuOp = item.sku;
this.nameOp = item.name;
}
我能够调用该函数,但我在console.log(item)中未定义
3 回答
有几件事情共同造成了这个错误 . 第一个改变是,而不是像这样使用
click
事件:您应该使用Ionic公开的
ionChange
事件:另请注意,由于您使用
[(ngModel)]="place"
将select元素绑定到您的某个组件's properties, you don' t需要将该项目作为参数发送,因为this.place
将在触发ionChange
事件时成为所选项目 .这就是为什么你的
optionsFn
方法看起来像这样:使用(ngModelChange)代替(click)事件 .
每当模型值改变时,ngModelChange将自动调用相对函数 .
进行以下更改后检查:
// html改变
//.ts改变