Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Angular: "btn" class disappear after toggled the button

I have an issue with the class changing button. I use the [ngClass] directive to change the button style ( Bootstrap classes). But when I click the button, the "btn" class disappears from the code, so instead get ‘btn-success btn’, I get only ‘btn-success’. The button also takes a part in showing and hiding a paragraph with random text.

This is TS code:

<button type="button"
  (click)="onClick($event)"
  [ngClass]="{'btn-success btn': showPass, 'btn-danger btn': !showPass }">{{status}}
</button>

<p [hidden]="showPass"  >Secret password</p>```

and this is HTML:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  onClick(event: Event) {

    this.showPass=!this.showPass;
    this.status = this.showPass ? "Show me!" : "Hide me!"
    console.log(this.showPass)

>Solution :

If the btn class is always going to be there, you can just do this instead:

<button type="button"
  (click)="onClick($event)"
  class="btn"
  [ngClass]="{'btn-success': showPass, 'btn-danger': !showPass }">{{status}}
</button>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading