How to create login in ionic 2 with show/hide password

前端 未结 3 971
深忆病人
深忆病人 2021-02-04 04:54

I wanted to create a design like this using ionic 2 -> https://codepen.io/Floky87/pen/bVopyZ

Which is a login functionality that has a hide/show password.

Here\

相关标签:
3条回答
  • 2021-02-04 05:10
    public type = 'password';
    public showPass = false;
    showPassword() {
       this.showPass = !this.showPass;
       if(this.showPass){
         this.type = 'text';
       } else {
         this.type = 'password';
       }
    }
    
    <ion-item class="i2"no-lines>
       <ion-input type="{{type}}" name="password" [(ngModel)]="password" placeholder="Password" style="width: 40px;" no-lines>
       </ion-input>
       <button *ngIf="!showPass" ion-button clear color="dark" type="button" item-right (click)="showPassword()"> 
          <ion-icon name="eye" style="font-size:25px;"></ion-icon>
       </button>
       <button *ngIf="showPass" ion-button clear color="dark" type="button" item-right (click)="showPassword()"> 
          <ion-icon name="eye-off" style="font-size:25px;"></ion-icon>
       </button>
    </ion-item>
    
    0 讨论(0)
  • 2021-02-04 05:17

    You can simply wrap it in a button to make it clickable:

    <ion-item>
      <ion-label floating primary>Password</ion-label>
      <ion-input type="password"></ion-input>
      <button ion-button clear item-end (click)='showHide()' style="height:32px;">
        <ion-icon name="eye" style="font-size:32px;"></ion-icon>
      </button>
    </ion-item>
    

    Use the style attributes to control the size of the icon.

    0 讨论(0)
  • 2021-02-04 05:24

    You can do like below in your .ts file write this code

     passwordType: string = 'password';
     passwordIcon: string = 'eye-off';
    
     hideShowPassword() {
         this.passwordType = this.passwordType === 'text' ? 'password' : 'text';
         this.passwordIcon = this.passwordIcon === 'eye-off' ? 'eye' : 'eye-off';
     }
    

    in your .html write this

    <ion-item>
        <ion-label floating>Password</ion-label>
        <ion-input formControlName="password" [type]="passwordType" clearOnEdit="false"></ion-input>
        <ion-icon item-end [name]="passwordIcon" class="passwordIcon" (click)='hideShowPassword()'></ion-icon>
    </ion-item>
    

    and this will be your .scss code. Change according to your need

    .passwordIcon{
       font-size:2rem !important;
       position: relative !important;
       top: 22px !important;
       margin: 0 auto !important;
    }
    
    0 讨论(0)
提交回复
热议问题