Ionic2 如何在ion select中获取所选项目的索引

Ionic2 如何在ion select中获取所选项目的索引,ionic2,ion-select,Ionic2,Ion Select,我正在使用作为下拉列表,它按预期工作,但我想要的是中所选选项的索引 我使用了并更改了如下事件: <ion-select class="myCustomSelect" [(ngModel)]="selectedProductDD" interface="popover" (ionChange)="onSelectChange($event)"> <ion-option *ngFor="let selectedProduct of productArray" [value]

我正在使用
作为下拉列表,它按预期工作,但我想要的是
中所选选项的索引

我使用了
并更改了如下事件:

<ion-select class="myCustomSelect" [(ngModel)]="selectedProductDD" interface="popover" (ionChange)="onSelectChange($event)">
    <ion-option *ngFor="let selectedProduct of productArray" [value] = selectedProduct.pid>{{ selectedProduct.pid }} </ion-option>
</ion-select>
如果有人能在这方面帮助我,请告诉我


提前感谢。

在您的
*ngFor
中,您还可以定义一个变量来接收索引

<ion-select class="myCustomSelect" [(ngModel)]="selectedProductDD" interface="popover" (ionChange)="onSelectChange($event)">
  <!-- you can also have your index by declaring a variable in ngFor that'll receive the index -->
  <ion-option *ngFor="let selectedProduct of productArray; let i = index" [value] = selectedProduct.pid>{{ selectedProduct.pid }} </ion-option>
</ion-select>
每次用户选择一个选项时,属性都会收到索引

<!-- on selecting an option your myIndex will receive the current selected option index -->
<ion-option *ngFor="let selectedProduct of productArray; let i = index" [value]=selectedProduct.pid (ionSelect)="myIndex = i">{{ selectedProduct.pid }}</ion-option>

还有其他选择,但这是最有角度的方法。希望这有帮助。

在您的
*ngFor
中,您还可以定义一个变量来接收索引

<ion-select class="myCustomSelect" [(ngModel)]="selectedProductDD" interface="popover" (ionChange)="onSelectChange($event)">
  <!-- you can also have your index by declaring a variable in ngFor that'll receive the index -->
  <ion-option *ngFor="let selectedProduct of productArray; let i = index" [value] = selectedProduct.pid>{{ selectedProduct.pid }} </ion-option>
</ion-select>
每次用户选择一个选项时,属性都会收到索引

<!-- on selecting an option your myIndex will receive the current selected option index -->
<ion-option *ngFor="let selectedProduct of productArray; let i = index" [value]=selectedProduct.pid (ionSelect)="myIndex = i">{{ selectedProduct.pid }}</ion-option>


还有其他选择,但这是最有角度的方法。希望这有帮助。

您只需要索引或带索引的值..带索引的值XHMM..您必须将自定义对象绑定到值..您不能使用
indexOf
?签出ionic 4中的解决方案并将其调整为ionic 2[解决方案以在ionic select中获取选定索引]()签出ionic 4中的解决方案并将其调整为ionic 2[在ion select中获取选定索引的解决方案]()您只需要索引或带有索引的值..带有索引的值XHMM..您必须将自定义对象绑定到值..您不能使用
indexOf
?签出ionic 4中的解决方案并将其调整为ionic 2[在ion select中获取选定索引的解决方案]()签出Ionic 4中的解决方案并将其调整为Ionic 2[解决方案以在ion select中获取选定索引]()上面的解决方案不起作用我在从下拉列表中选择另一个选项时出错你在代码上面测试了吗?我得到
未定义的选定索引
@CodeChanger哦,很抱歉,
myIndex
不需要接收
I
索引
我已根据你的要求进行了更改,更新了所有内容,但选定了一个值给出错误的索引有什么想法吗?第一个选项是给出错误的索引?在声明myIndex时尝试删除
=0
。上述解决方案不起作用。我在从下拉列表中选择另一个选项时出错。您在代码上方测试过吗?我得到
未定义的选定索引
@CodeChanger哦,很好,
myIndex
不需要接收
i
而不是
索引
我已根据您的更新进行了更改一切正常,但1个选定值给出了错误的索引对此有何想法?第一个选项是给出错误的索引?在声明myIndex时尝试删除
=0
onSelectChange(selectedValue: any) {
  let index = this.myIndex;
}