show/hide password text using angular2

后端 未结 8 1333
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-15 11:53

i want to show/hide the password text based on the user click. But i am getting the following error saying:

export class App {
    password = \"secret\"         


        
相关标签:
8条回答
  • 2021-02-15 12:15

    You could do something like this:

    In your App class.

       _show = false
       _pwdType = 'pwd'
    
       toggleShow() {
    
          this._show = !this._show
          this._pwdType = this._show ? 'text' : 'password'
    
       }//toggleShow
    

    In your html

     <input [type]="_pwdType" class="form-control" ...etc">
    

    Having said that I like @codtex's answer better

    0 讨论(0)
  • 2021-02-15 12:16

    You need to use ViewChild() instead of ContentChild()

    @ViewChild(ShowHideInput) input: ShowHideInput;
    

    check your plunk

    ContentChild() is for matching inside the transcluded content. e.g. contents inside <ng-content></ng-content>

    0 讨论(0)
  • 2021-02-15 12:18

    I see that in the approach from the question functionality is implemented directly in the component where the <input> is and just for the record I want to share a more elegant approach - creating a component for the purpose

    Check the demo plunkr HERE, can be useful for others.

    Cheers!

    0 讨论(0)
  • 2021-02-15 12:23
    //In HTML
    <div>
    <input [type] = "passType" placeholder="password">
    </div>
    
    <input type = "checkbox" (click)="changePasswordType()">Show Password
    
    //In TS file
    passType: string = 'password';
    
    changePasswordType(){
    if(this.passType== 'password'){
    this.passType= 'text'
    }else{
    this.passType== 'password'
    }
    }
    
    0 讨论(0)
  • 2021-02-15 12:26

    This is a better answer. Check below:

    input

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

    click action

    <button (click)="password()">Show</button>
    

    component

    // variable
    show: boolean;
    
    constructor() {
       // initialize variable value
       this.show = false;
    }
    
    // click event function toggle
    password() {
        this.show = !this.show;
    }
    
    0 讨论(0)
  • <input [type]="show_button ? 'text' : 'password'">
    <i [class]="show_eye ? 'fa fa-eye' : 'fa fa-eye-slash'" (click)="showPassword()"> // Modify style as you need
    
     // variable
      show_button: Boolean = false;
      show_eye: Boolean = false;
    
    //Function
    showPassword() {
        this.show_button = !this.show_button;
        this.show_eye = !this.show_eye;
      }
    
    0 讨论(0)
提交回复
热议问题