在Ionic 2中使用数组进行警报

div*_*hch 3 typescript ionic-framework ionic2 ionic3 angular

如何使用数组填充警报列表.例如,如果数组有一堆名称.

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

我希望弹出警报并显示这些名称,以便从中进行选择.我正在使用离子2.

seb*_*ras 12

由于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();
  }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助 :)

  • 也适合任何想要这样做的人.您不能在同一警报中同时拥有无线电项目和常规输入.这只是离子2的一个问题我想我在一个论坛上看到他们可能会更新它.截至目前,只发两个警报. (2认同)