2016-08-25 72 views
2

如何使用陣列填充警報列表。例如,如果數組有一堆名稱。Ionic 2中的陣列警報

array = [john,dixy,tom,jared];

我想彈出警告並顯示這些名稱,以便從中進行選擇。 我正在使用離子型2.

+0

@sebaferreras我已經嘗試了for循環警報來填充警報內的電臺列表內的陣列,但不起作用。它給了我一個錯誤,不把for循環放在alert中。但是如果我把for循環放在它的外面,會給我陣列警報的大小。 – divyanshch

+0

好的,謝謝。你是否想要實現類似[this](http://ionicframework.com/docs/v2/api/components/select/Select/#single-value-radio-buttons)? – sebaferreras

+0

是和不是。我正在尋找,但在一個警報不在其自己的頁面 – divyanshch

回答

9

由於alert是通過使用帶有選項的對象創建的,所以我們可以使用該對象創建帶有數組名稱的單選按鈕。

import { Component } from "@angular/core"; 
import { AlertController } from "ionic-angular/index"; 

@Component({ 
    templateUrl:"home.html" 
}) 
export class HomePage { 

    private names: Array<string>; 

    constructor(private alertCtrl: AlertController) { 
    this.names = [ 'john', 'dixy', 'tom', 'jared']; 
    } 

    presentConfirm() { 

    // Object with options used to create the alert 
    var options = { 
     title: 'Choose the name', 
     message: 'Which name do you like?', 
     buttons: [ 
     { 
      text: 'Cancel', 
      role: 'cancel', 
      handler:() => { 
      console.log('Cancel clicked'); 
      } 
     }, 
     { 
      text: 'Ok', 
      handler: data => { 
      console.log(data); 
      } 
     } 
     ] 
    }; 

    options.inputs = []; 

    // Now we add the radio buttons 
    for(let i=0; i< this.names.length; i++) { 
     options.inputs.push({ name : 'options', value: this.names[i], label: this.names[i], type: 'radio' }); 
    } 

    // Create the alert with the options 
    let alert = this.alertCtrl.create(options); 
    alert.present(); 
    } 
} 

希望這有助於:)

+2

也爲任何人試圖做到這一點。您無法在同一個警報中同時收聽廣播項目和常規輸入。這只是一個關於離子2的問題,我想我在一個論壇上讀到他們可能會更新它。截至目前,只需發出兩次警報。 – divyanshch