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";
show = false;
@ContentChild(ShowHideInput) input: ShowHideInput;
constructor(){}
toggleShow()
{
this.show = !this.show;
console.log(this.input); //undefined
if (this.show){
this.input.changeType("text");
}
else {
this.input.changeType("password");
}
}
}
The following plunker link which i have created.
https://plnkr.co/edit/2GK79PuPtRQNmoUbF6xC?p=preview
Use the tag <i> to display the eye icon. This icon is also known as the visibility eye icon. Use the below CSS to put the eye icon at the end of the password text field.
This is a better answer. Check below:
input
<input [type]="show ? 'text' : 'password'" />
click action
<button (click)="password()">{{show ? 'Show' : 'Hide'}}</button>
component
// variable - default false
show: boolean = false;
constructor() {
}
// click event function toggle
password() {
this.show = !this.show;
}
You could also do all of this in the template in a quick and dirty way..
<input #x type="password">
<button (click)="x.type=x.type=='password'?'text':'password'"></button>
<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;
}
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>
in you html :
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" #password-field>
<span (click)="password-field.type=password-field.type=='password'?'text':'password'"
class="fa fa-fw field-icon toggle-password"
[ngClass]="(password-field.type=='password')?' fa-eye':' fa-eye-slash'"></span>
</div>
in your css or sass
.field-icon {
float: right;
left: -15px;
margin-top: -25px;
position: relative;
z-index: 2;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With