【问题标题】:Ionic 3 - set id value in alert controllerIonic 3 - 在警报控制器中设置 id 值
【发布时间】:2018-11-03 07:06:30
【问题描述】:

是否可以在 ionic 3 的警报控制器中设置 id 值?

通常在 html 标签中我们可以设置id 值。但是在这里显示警报对话框时,我们在 ts 代码中使用警报控制器。那么有没有办法在警报控制器中设置id

【问题讨论】:

    标签: typescript ionic3


    【解决方案1】:

    试试下面的例子并检查文档here

    import { AlertController } from 'ionic-angular';
    
    constructor(private alertCtrl: AlertController) {
    
    }
    
    presentAlert() {
      let alert = this.alertCtrl.create({
        title: 'Low battery',
        subTitle: '10% of battery remaining',
        buttons: ['Dismiss']
      });
      alert.present();
    }
    
    presentConfirm() {
      let alert = this.alertCtrl.create({
        title: 'Confirm purchase',
        message: 'Do you want to buy this book?',
        buttons: [
          {
            text: 'Cancel',
            role: 'cancel',
            handler: () => {
              console.log('Cancel clicked');
            }
          },
          {
            text: 'Buy',
            handler: () => {
              console.log('Buy clicked');
            }
          }
        ]
      });
      alert.present();
    }
    
    presentPrompt() {
      let alert = this.alertCtrl.create({
        title: 'Login',
        inputs: [
          {
            name: 'username',
            placeholder: 'Username'
          },
          {
            name: 'password',
            placeholder: 'Password',
            type: 'password'
          }
        ],
        buttons: [
          {
            text: 'Cancel',
            role: 'cancel',
            handler: data => {
              console.log('Cancel clicked');
            }
          },
          {
            text: 'Login',
            handler: data => {
              if (User.isValid(data.username, data.password)) {
                // logged in!
              } else {
                // invalid login
                return false;
              }
            }
          }
        ]
      });
      alert.present();
    }
    

    【讨论】:

    • 在这里我们为警报控制器及其按钮设置 id。像 id="mainalert" 和按钮 id="cancelBtn" & id="buyBtn" 等。我知道下面的代码是不可能的。但无论如何要实现这一点。 let alert = this.alertCtrl.create({ title: 'Low battery', subTitle: '10% of battery remaining', id: 'alertCtrl', buttons: [ { text: 'Cancel', id: 'cancelBtn', role: 'cancel', handler: data => { console.log('Cancel clicked'); } } }); alert.present();
    • 有人知道这个问题吗?
    猜你喜欢
    • 2019-09-05
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多