I am having trouble checking the active state of an element attribute. I tried below but it returned false even though the element has the attribute in an active state - (.c-banner.active
is present)
ngAfterViewInit() {
const bannerElm = document.getElementById("banner");
const isActive = bannerElm && bannerElm.getAttribute("class")
.indexOf("c-banner.active") !== -1; // returned false
}
You can use classList.contains
method to check if the element has the active class.
ngAfterViewInit() {
setTimeout(() => {
const isActive = bannerElm &&
bannerElm.classList.contains('c-banner') &&
bannerElm.classList.contains('active');
}, 1000);
}
[UPDATED] wrap it within setTimeout() and it worked! In case anyone else is stuck with the component initialization orders issue that I had previously.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments