简体   繁体   English

隐藏显示密码在angular2 formBuilder中不起作用

[英]Hide show password is not working in angular2 formBuilder

Issue : When i toggle between show and hide password , the entered values of formControlName 'Password' is not reflecting. 问题 :当我在显示和隐藏密码之间切换时,formControlName'Password'的输入值无法反映。

flow : 流量

  1. enter some values in password field . 在密码字段中输入一些值。
  2. toggle the field using show password 使用显示密码切换字段
  3. password field is empty 密码字段为空

my view part : 我的看法部分:

 <ion-input formControlName="password" placeholder="Password*" type="password" (ngModelChange)="onChange($event)"
                   autocomplete="off" [hidden]="showPasswordModel"></ion-input>

    <ion-input formControlName="password" placeholder="Password-" type="text" (ngModelChange)="onChange($event)"
               autocomplete="off" [hidden]="!showPasswordModel"></ion-input>


                <ion-item class="checkbox-container">
    <ion-label>Show password</ion-label>
    <ion-toggle  (click)="showPasswordModel = !showPasswordModel"></ion-toggle>
  </ion-item>

ts section ts部分

 constructor(private _formBuilder:FormBuilder) {

     this.loginForm = this._formBuilder.group({      
          password: ['', Validators.compose([Validators.required])]      
        });
--
}

hint : http://blog.ng-book.com/the-ultimate-guide-to-forms-in-angular-2/ 提示: http : //blog.ng-book.com/the-ultimate-guide-to-forms-in-angular-2/

Thanks in advance 提前致谢

Here you can see using type = Password HTML5 on element 在这里您可以看到在元素上使用type = Password HTML5

Input Field and button to play in HTML and #input in id to identify it 输入字段和按钮以HTML格式播放,#input以id标识

 <ion-input #input formControlName="password" type="password"></ion-input>
 <button class="btn" type="button" (click)="showPassword(input)"> Show Password </button>

The .ts file function will be something Like this .ts文件功能将如下所示

showPassword(input: any): any {
   input.type = input.type === 'password' ?  'text' : 'password';
  }

I know this was marked as correct, but I found a way to do this by keeping a toggle icon next to the input. 我知道这被标记为正确,但是我找到了一种方法,可以在输入旁边保留一个切换图标。

I have an icon that displays an eye icon, ios-eye-outline , when the input type is set to password and a eye with a line through icon, ios-eye-off-outline , when the input type is set to text . 我有一个图标,当输入类型设置为password时,它显示一个眼睛图标ios-eye-outline ,而当输入类型设置为text时,它带有一个穿过图标ios-eye-off-outline

I used JavaScript to manipulate the input type, I also used JavaScript to get the current input type and decided to do the compare on that. 我使用JavaScript来操纵输入类型,还使用JavaScript来获取当前输入类型,并决定对此进行比较。

The showPassword boolean is used to hide and display the different icons. showPassword布尔值用于隐藏和显示不同的图标。

The icon also will only display once the user has entered something into the password field, and hide it again when the field is empty. 仅当用户在密码字段中输入了内容后,该图标才会显示,并且在该字段为空时再次将其隐藏。

Inside my *.html 在我的*.html

<ion-item>
  <ion-label floating color="dark">Password</ion-label>
  <ion-input id="password" [(ngModel)]="login.password" name="password" type="password" #password="ngModel" required>
  </ion-input>
  <button ion-button clear color="dark" type="button" class="password__btn__toggle" item-right (click)="togglePassword()" *ngIf="!showPassword && login.password"> <ion-icon id="password__icon" name="ios-eye-outline"> </ion-icon> </button>
  <button ion-button clear color="dark" type="button" class="password__btn__toggle" item-right (click)="togglePassword()" *ngIf="showPassword && login.password"> <ion-icon id="password__icon" name="ios-eye-off-outline"> </ion-icon> </button>
</ion-item>
<p [hidden]="password.valid || submitted == false" color="danger" padding-left>
  Password is required
</p>

inside my *.ts 在我的*.ts

public showPassword: boolean = false;

public togglePassword(input:any): void {
  let currentType:string = document.getElementById('password').querySelector('.text-input').getAttribute('type');

  if (currentType === 'password') {
    this.showPassword = true;
    document.getElementById('password').querySelector('.text-input').setAttribute('type', 'text');
  } else {
    this.showPassword = false;
    document.getElementById('password').querySelector('.text-input').setAttribute('type', 'password');
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM