ionic2 - show ion-icon in alert controller title a

2019-08-23 01:48发布

In ionic 2 application, is that possible to add ion-icon in the alert controller like shown below

    let showAlertCtrl = this.alertCtrl.create({
  title: '<ion-icon ios="ios-add" md="md-add"></ion-icon>' +'Add your info',
  cssClass: 'myCustomCSS',
  message: msg,
  enableBackdropDismiss: false,
  buttons: [
    {
      text: '<ion-icon ios="iOS-search" md="md-add"></ion-icon>' +' Ok',
      cssClass: 'customAlertBtn',
      handler: (data: any) => {

      }
    },]
});

showAlertCtrl.present();      

Need to display ion-icon image on both title and buttons. Is that possible?

Also I knew that we can use modal to achieve this. But my first priority to check it in alert controller.

Thanks AK

3条回答
我命由我不由天
2楼-- · 2019-08-23 02:30

You should use backticks to parse the html:

title: `<ion-icon ios="ios-add" md="md-add"></ion-icon>Add your info`
查看更多
\"骚年 ilove
3楼-- · 2019-08-23 02:31

If you decide to use a modal, you can make it look like an alert with css.For example:

    var z: any = document.getElementsByTagName("ion-modal");
    z[0].style.background = "rgba(0,0,0,0.8)";
    z[0].style.padding = "36px";
    z[0].style.paddingTop = "150px";
    z[0].style.paddingBottom = "226px";
    z[0].childNodes[1].style.borderRadius = "6px";
查看更多
一夜七次
4楼-- · 2019-08-23 02:41

A bit late response. I tried to achieve this today, and I made it like this:

Create a css class in your app.scss and add that class in the alert option "cssClass".

app.scss

.yourClass{
  background-image: url(your-image-url);
  background-position: center;
  background-repeat: no-repeat;
  background-size: contain;  
}

These css values can be changed per your requirements.

And in the Alert:

buttons: [
           {
             text: '',
             cssClass: 'yourClass',
             handler: data => {}
          }
        ]
查看更多
登录 后发表回答