如何在Angular指令中获取元素的当前类 [英] How to get the current classes of an element in an Angular directive
本文介绍了如何在Angular指令中获取元素的当前类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须做出一个Angular 7指令,在这里我可以从元素(ElementRef)切换(添加/删除)一个类。当不在元素中的类添加元素时,将其删除。
I have to make an Angular 7 directive where I can toggle (add/remove) a class from the element (ElementRef). When the class in not in the element add it, and when it's in the element remove it.
我知道如何添加/删除类...但是如何得到一个元素的当前类? ...这样我就可以检查是否必须添加或删除
I know how to add / remove a class ... but how to get the current classes of an element ? ... so I can check if I have to add or remove
推荐答案
要获取元素的当前类,您可以做:
To get the current classes of an element, you could do:
const elementRef: ElementRef;
const classes = elementRef.nativeElement.classList;
if (classes.contains('your class')
{
// do something
}
这篇关于如何在Angular指令中获取元素的当前类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文