2017-10-18 65 views
0

標題說明了所有內容。我必須在Ionic中做一個相對簡單的混合應用程序,並且我想在按下按鈕時在幾個離子選擇中獲得選定的值。這裏有雲代碼:獲取從列表中選擇的值 - Ionic

page.html中

<ion-content padding> 

    <div id="sectionTitle"> </div> 
    <ion-list> 
    <ion-item *ngFor="let question of questions" (click)="itemSelected(question)"> 
     <ion-label>{{question.texto}} </ion-label> 
     <ion-select> 
     <option ng-selected="selected">{{answers[0].texto}}</option> 
     <ion-option text-wrap required *ngFor="let answer of answers; let i = index" onchange="itemSelected(answer)"> 
      <p> {{answer.text}} </p> 
     </ion-option> 
     </ion-select> 
    </ion-item> 
    </ion-list> 

    <div class="sendButton"> 
    <button ion-button full type="submit" (click)="toComparisonPage()"> 
    Send 
<!-- Here is where data must be retrieved --> 
    </button> 
    </div> 
</ion-content> 

page.ts

itemSelected(answer){ 
    console.log("Selected item: "); 
} 

在此先感謝!

回答

0

你在你的標籤上指定一個ngModel。

<ion-select [(ngModel)]="selectedItems"> 

在你的page.ts中你應該有一個公共變量聲明並且可以訪問它。

console.log(this.selectedItems);

更多的選擇配置水頭高達離子文檔 https://ionicframework.com/docs/api/components/select/Select/

+0

爲之前嘗試,我也指定了ngModel,但由於這些選擇都dynamicaly創建的,我想他們分享ngModel,所以當它們中的一個被修改時,它們都會自動更改:S – camnuel

+0

以及您可以擁有一個對象/數組並將您的值存儲在* ngFor的相應索引中。 例如: '* ngFor =「讓問題提問;讓我=索引'' 然後用 'ng-selected =」selected [i]「' –