从Ionic 4警报的输入中获取价值

And*_*ndi 4 ionic4

到目前为止,有关离子警报的Ionic 4文档包含一个示例如何向警报添加文本输入,例如:

const alert = await this.alertController.create({
  inputs: [
    {
      name: 'name1',
      type: 'text'
    },
Run Code Online (Sandbox Code Playgroud)

但是我找不到如何从name1文本输入中访问值的方法,例如在按钮处理程序中。

我是否必须使用类似的东西,document.getElementById还是有其他的离子/角度样式方式?

Ira*_* JW 12

您可以在警报关闭处有一个按钮来处理数据。

   const alert = await this.alertController.create({
      inputs: [
        {
          name: 'name1',
          type: 'text'
        }],    
       buttons: [
            {
              text: 'Cancel',
              role: 'cancel',
              cssClass: 'secondary',
              handler: () => {
                console.log('Confirm Cancel');
              }
            }, {
              text: 'Ok',
              handler: (alertData) => { //takes the data 
                console.log(alertData.name1);
            }
            }
          ]
  });
  await alert.present();
Run Code Online (Sandbox Code Playgroud)


ben*_*-we 7

其实可以简写为:

const alert = await this.alertController.create({
    header: 'Prompt!',
    inputs: [
        {
            name: 'input1',
            type: 'text',
            placeholder: 'Please enter text'
        }
    ],
    buttons: [
        {
            text: 'Cancel',
            role: 'cancel',
            cssClass: 'secondary',
            handler: () => {
                console.log('Confirm Cancel');
            }
        }, {
            text: 'Ok',
            handler: (alertData) => {
                console.log(alertData.input1);
            }
        }
    ]
});
Run Code Online (Sandbox Code Playgroud)


虽然参数alertData只包含输入及其值:

<code>alertData</code> 在控制台