【问题标题】:Alerts with array in Ionic 2Ionic 2中的数组警报
【发布时间】:2017-01-02 09:51:56
【问题描述】:

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

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

我希望弹出警报并显示这些名称,以便从中选择它们。 我正在使用 ionic 2。

【问题讨论】:

  • @sebaferreras 我已经为警报内的数组尝试了一个 for 循环,以填充警报内的无线电列表,但这不起作用。它给了我一个错误,没有将 for 循环放在警报中。但是如果我把 for 循环放在外面,它会给我数组警报的大小。
  • 好的,谢谢。您是否正在尝试实现this 之类的目标?
  • 是和不是。我正在寻找它,但在警报中不是它自己的页面

标签: angular typescript ionic-framework ionic2 ionic3


【解决方案1】:

由于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();
  }
}

希望这会有所帮助:)

【讨论】:

  • 也适用于任何尝试这样做的人。您不能在同一个警报中同时拥有单选项目和常规输入。这只是 ionic 2 的一个问题,我想我在论坛上读到过,他们可能会更新它。截至目前,只需发出两个警报。
猜你喜欢
  • 1970-01-01
  • 2017-09-15
  • 2017-03-20
  • 2017-12-29
  • 2016-12-23
  • 2017-06-15
  • 1970-01-01
  • 1970-01-01
  • 2018-10-23
相关资源
最近更新 更多