离子2离子选择 - 以编程方式关闭选择框

use*_*143 3 ionic2

我正在研究ionic2项目.我使用离子选择元素.

我搜索了一种方法,以编程方式关闭选择框选择任何项目(而不是等待用户按OK).

  <ion-select id="select" #select>
      <ion-option (ionSelect)="closeAndSave()" *ngFor="let option of enumList" [value]="option">{{ option}}</ion-option>
    </ion-select>
Run Code Online (Sandbox Code Playgroud)

`

class myClass{
    @viewChild('select') select:Select;
    closeAndSave(){
        /*it is come here on press any option. but how can I close here my select element? I tried: this.select.destroy()  - not do any thing. any solution?*/
    }
Run Code Online (Sandbox Code Playgroud)

`

小智 7

这就是我做的:

this.court = val;
this.select.close();
Run Code Online (Sandbox Code Playgroud)

把它放在你的closeAndSave功能里面.

在此之前,您需要从closeAndSave函数传递一些值,如:

(ionSelect)="closeAndSave('someValueHere')"
Run Code Online (Sandbox Code Playgroud)

然后在你的TS代码中使用这个值:

import { Select } from 'ionic-angular';

class myClass{
    @ViewChild(select) select:Select;
    closeAndSave(val){
    this.someVar = val;
        this.select.close();
    }
}
Run Code Online (Sandbox Code Playgroud)