【问题标题】:How Get Other Value from select option ionic 3如何从选择选项 ionic 3 中获取其他价值
【发布时间】:2020-03-12 03:55:49
【问题描述】:

我有这样的东西 .html

<ion-item>
    <ion-label>Choose Industry Type</ion-label>
    <ion-select (ionChange)="getOuterName()" [(ngModel)]="getSelectedValue">
        <ion-option *ngFor="let some of someItems" value="{{some.id}}">
            {{some.name}} </ion-option>
    </ion-select>
</ion-item>
<p>{{getSelectedValue}}</p>

.ts

someItems = [{'id': 1, 'name':'Agriculture','othervalue':'123'},
          {'id': 2, 'name':'Chemicals','othervalue':'1234'},
          {'id': 3, 'name':'Pesticides','othervalue':'12345'}];
getOuterName(){
 console.log(this.getSelectedValue);
 let term = this.someItems.filter(item => item.othervalue ===this.getSelectedValue );
}

我可以在 'getSelectedValue' 中显示 'othervalue' 而不更改 value="{{some.id}}"离子选项?

【问题讨论】:

  • 那么现在显示的是什么值?因为你使用过 ngModel,它应该显示值。

标签: ionic-framework ionic3


【解决方案1】:

你可以做的只是将整个对象绑定到value

html:-

<ion-item>
        <ion-label>Choose Industry Type</ion-label>
        <ion-select (ionChange)="getOuterName()" [(ngModel)]="getSelectedValue"  >
            <ion-option *ngFor="let some of someItems" [value]="some" >
                {{some.name}} </ion-option>
        </ion-select>
    </ion-item>

在 .ts:-

 someItems = [{'id': 1, 'name':'Agriculture','othervalue':'123'},
          {'id': 2, 'name':'Chemicals','othervalue':'1234'},
          {'id': 3, 'name':'Pesticides','othervalue':'12345'}];

getOuterName(){
// Your can call your **this.getSelectedValue** and its properties
 console.log( this.getSelectedValue.id + this.getSelectedValue.name);

 let term = this.someItems.filter(item => item.othervalue ===this.getSelectedValue.othervalue );
}

这样你就可以同时拥有idothervalue

【讨论】:

  • 我得到了这个:/turbo_modules/@angular/compiler@5.2.9/bundles/compiler.umd.js (528:34) 模板解析错误:无法绑定到“值”,因为它不是“离子选择选项”的已知属性。 1. 如果 'ion-select-option' 是一个 Angular 组件并且它有 'value' 输入,那么验证它是这个模块的一部分。 2. 如果“ion-select-option”是 Web 组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到该组件的“@NgModule.schemas”以禁止显示此消息。
  • 对不起,这是我的错误,我在 ionic 4 上工作,所以在 ionic 4 中测试了代码,我们使用 ion-select-option 而不是 ion-option(ionic 3 变体)。只需使用ion-option 即可。我也更新了上面的代码。
猜你喜欢
  • 2018-02-03
  • 1970-01-01
  • 2018-05-17
  • 1970-01-01
  • 2015-08-07
  • 2018-03-08
  • 2014-07-12
  • 1970-01-01
  • 2020-06-02
相关资源
最近更新 更多