2017-12-28 7 views
0

Ist es möglich, das Attribut eines HTML-Elements mit Code in Angular zu ändern? Ich habe eine Schaltfläche, die ich möchte, um das Typ-Attribut einer Eingabe von Passwort in Text zu ändern. Dies ist, was kam zuerst in den Sinn:Wie ändert man das Attribut eines Elements in Angular 2

Vorlage:

<input name="password" type="password" /> 

<button click="showPassword()">eye</button> 

Komponente:

showPassword() : void 
{ 
     //how do I change the password input to text input. Is there a better way to do this? 
    } 

Antwort

2
<input name="password" [type]="password" /> 

In ts Datei

public password='password'; 
showPassword() : void 
{ 
     this.password=(this.password=='password')?'text':'password'; 
} 
1

In Ihrem HTML,

<input name="password" [type]='password' /> 

<button click="showPassword()">eye</button> 

In Ihrem TS,

export class ClassName { 
    password: String = 'password'; 
    showPassword() : void{ 
     this.password = (this.password=='password')?'text':'password'; 
    } 
} 
Verwandte Themen