user3607282 user3607282 - 21 days ago 28
CSS Question

Ionic 2 - Cannot add an ion-icon inside an ion-item containing a ion-input

I'm using Ionic 2 and on my app I'm creating a form where if there is a validation error, an information icon will appear on the right side of it's relevant input field. The HTML is as follows,

<ion-list inset padding>
<ion-item>
<ion-input type="text" placeholder="Email"></ion-input>
<ion-icon name="ios-information-circle-outline" item-right></ion-icon>
</ion-item>
</ion-list>


This works perfectly fine. But whenever I add an *ngIf onto the
ion-icon
, it disappears from the ui. Here is a sample where I've set *ngIf to true to see if it works. The icon does not show.

<ion-list inset padding>
<ion-item>
<ion-input type="text" placeholder="Email"></ion-input>
<ion-icon name="ios-information-circle-outline" item-right *ngIf="true"></ion-icon>
</ion-item>
</ion-list>


When the
<ion-input>
is removed from above example, the
<ion-icon>
shows.

Is this a restriction in Ionic 2? How do I add an icon inside an
ion-item
which contains an
ion-input
?

Answer

Please try ngClass as a workaround for this issue

    <ion-item> 
        <ion-input type="text" placeholder="Email"></ion-input>
        <ion-icon [ngClass]="{'hide': true }" name="ios-information-circle-outline" ></ion-icon>
    </ion-item>

    <style>.hide { display:none; } </style>