Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

Angular on icon click toggle class name or part of the class name

I am using Angular 11 and in my component.html file I have an icon:

<i class="fa fa-eye" aria-hidden="true"></i>

What I need to do is to toggle the class when I click on it.

I need to toggle between:

<i class="fa fa-eye aria-hidden="true"></i>

and

<i class="fa fa-eye-slash aria-hidden="true"></i>

How can I do this?

question from:https://stackoverflow.com/questions/65848916/angular-on-icon-click-toggle-class-name-or-part-of-the-class-name

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

.ts file :

@Component
export class YourComponent extends ... {
   toggle:boolean = false; // add any public variable
}

.html file :

<i [ngClass]="toggle ? 'fa-eye' : 'fa-eye-slash'" class="fa" aria-hidden="true"
(click)="toggle = !toggle"></i>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...