ionic 如何获得 select 的值

ionic how to get value of select

html:
<ion-list>
  <ion-item>
    <ion-label><b>Select</b></ion-label>
    <ion-select [compareWith]="compareWith">
      <ion-select-option *ngFor="let item of items">{{item.title}}</ion-select-option>
    </ion-select>
  </ion-item>
</ion-list>

typescript:

items:any;
      compareWithFn = (o1, o2) => {
        return o1 && o2 ? o1.id === o2.id : o1 === o2;
      };

      compareWith=this.compareWithFn;
      constructor(public http:HttpClient) {
        this.loadData()
      }
      loadData(){
        let data:Observable<any>
        const headers = new HttpHeaders;
        data=this.http.get('https://jsonplaceholder.typicode.com/posts')
        data.subscribe(result=>{
          this.items=result
          console.log(result)
          console.log(result.valid)
        })
      }

它们与我的应用程序代码相同 我有列表 select 的标题,我会得到它的价值 selected

如何获取列表的 selected 值?

使用数据绑定保存select元素的值

<ion-select [compareWith]="compareWith" [(ngModel)]="mySelectValue">
      <ion-select-option *ngFor="let item of items">{{item.title}}</ion-select-option>
</ion-select>

打字稿:

mySelectValue:any; //Declare a variable mySelectValue

let value=this.mySelectValue;//Use the value of select

此外, 如果要在 select 值更改时调用函数,请使用 (ionChange)="myfunction()"

<ion-select [compareWith]="compareWith" [(ngModel)]="mySelectValue" (ionChange)="myfunction()" >