angular – 如何从ion-select选项中获取值

我有一个名为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}} &nbsp;&nbsp;{{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;
  }

i am able to invoke the function but i am getting undefined in console.log(item)

有几件事情共同造成了这个错误.
一个更改是,而不是像这样使用click事件:
(click)="optionsFn(item);

您应该使用Ionic公开的ionChange事件,如下所示:

(ionChange)="optionsFn();"

另请注意,由于您使用[(ngModel)] =“place”将select元素绑定到组件的某个属性,因此您无需将该项目作为参数发送,因为this.place将是所选项目何时触发ionChange事件.

这就是你的optionsFn方法看起来像这样的原因:

public optionsFn(): void { //here item is an object 
    console.log(this.place);

    let item = this.place; // Just did this in order to avoid changing the next lines of code :P

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

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...