单击复选框时如何显示输入的密码?

时间:2019-04-26 14:08:57

标签: angular7

在我的表单中,我有一个复选框,因此当我选中该复选框时,它应该显示在密码字段中输入的密码。我正在使用角度7进行操作。

  <div class="form-group">
  <label for="Password" class="control-label">Password</label>
  <div>
  <input type="password" class="form-control" name="Password" ngModel 
  #pwd="ngModel" placeholder="Password" required  pattern="^(?=.*[a-z])(? 
  =.* 
    [A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$">
    <div *ngIf= pwd.invalid>
      <small class="form-text text-danger" *ngIf= 
     pwd.errors.required>Password Required</small>
      <small class="form-text text-danger" 
      *ngIf=pwd.errors.pattern>Minimum eight characters, at least one 
      uppercase letter, one lowercase letter, one number and one special 
       character.</small>


  </div>
</div>
</div>
<div class="form-group form-check">
  <input type="checkbox" class="form-check-input" 
    [disabled]="pwd.invalid">
  <label class="form-check-label" id="show" for="exampleCheck1">Show 
  Password</label>
</div>

通过选中复选框,它应该显示在“密码”字段中输入的密码。

1 个答案:

答案 0 :(得分:0)

嗨,欢迎来到Stackoverflow!

尝试根据复选框的值在type =“ password”和type =“ text”之间切换。

将#showPassword添加到您的复选框输入中,就像这样:

<input #showPassword type="checkbox" class="form-check-input" [disabled]="pwd.invalid">

然后在您的密码字段上:

<input type="showPassword.checked ? 'text' : 'password'" .... />

编辑(如果您尚未解决,以及其他问题):

component.ts

// show/hide boolean variable
showPassword: boolean;

constructor() {
  // init value in constructor
  this.showPassword = false;
}

// click event, show/hide based on checkbox value
showHidePassword(e) {
    this.showPassword = e.target.checked;
}

component.html

<input [type]="showPassword ? 'text' : 'password'" />

<input type="checkbox" (change)="showHidePassword($event)" > toggle view