Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 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 Reply

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...