本文介绍了Angular ngClass和click事件切换类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Angular中,我想使用ngClass和click事件来切换类.我在网上浏览了一下,但其中一些是angular1,并且没有任何清晰的说明或示例.任何帮助将不胜感激!
In Angular, I would like to use ngClass and click event to toggle class. I looked through online but some are angular1 and there isn't any clear instruction or example. Any help will be much appreciated!
在HTML中,我有以下内容:
In HTML, I have the following:
<div class="my_class" (click)="clickEvent($event)" ngClass="{'active': toggle}">
Some content
</div>
在.ts中:
clickEvent(event){
//Haven't really got far
var targetEle = event.srcElement.attributes.class;
}
推荐答案
这应该对您有用.
在.html中:
<div class="my_class" (click)="clickEvent()"
[ngClass]="status ? 'success' : 'danger'">
Some content
</div>
在.ts中:
status: boolean = false;
clickEvent(){
this.status = !this.status;
}
这篇关于Angular ngClass和click事件切换类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!