如何在 Ionic 2 的警报对话框中设置警报消息的文本字段?

How do I set text field for alert message in the alert dialog in Ionic 2?

我正在使用 Firebase 收到推送通知并将其显示在警告框中。现在我想在文本字段中显示我收到的消息,以便用户可以编辑 message.I 也想在控制台中输出消息。

pushObject.on('notification').subscribe((notification: any) => {
  if (notification.additionalData.foreground) {
    let youralert = this.alertCtrl.create({
      title: 'New Push notification',
      message: notification.message,
      buttons: [
        {
          text: 'Cancel',
          role: 'cancel',
          handler: () => {
           console.log('Cancel clicked');
          }
        },
        {
          text: 'Okay',
          handler: () => {
            console.log('Okay clicked');
          }
        }
      ]
    });

Alert 界面上有一个 inputs 属性,它的工作原理与 buttons 非常相似。它是一个对象数组,您有一个输入 value 属性 来设置所需的值。

因为我不知道你想在哪里记录你的值,如果它是来自服务器的值或编辑后的值,我会显示两者。

pushObject.on('notification').subscribe((notification: any) => {
  if (notification.additionalData.foreground) {
    console.log('push message', notification.message);
    let youralert = this.alertCtrl.create({
      title: 'New Push notification',
      inputs: [{
        placeholder: 'Your placeholder..',
        type: 'text',
        name: 'yourInputName, // Name to get it in your handler callback
        value: notification.message
      }],
      message: notification.message,
      buttons: [
        {
          text: 'Cancel',
          role: 'cancel',
          handler: () => {
           console.log('Cancel clicked');
          }
        },
        {
          text: 'Okay',
          // you'll need to get the input data, so pass a parameter to the callback
          handler: (data) => {
            // here's the value user has edited in the input
            console.log('Edited message', data.yourInputName);
            console.log('Okay clicked');
          }
        }
      ]
    });

希望对您有所帮助