Last active
July 28, 2019 19:51
-
-
Save sandipchitale/63a089dcf0e267be6eb9cbc3df9d85bf to your computer and use it in GitHub Desktop.
angularjs directive to monitor class attribute
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.directive('class', function() { | |
return { | |
restrict: 'A', | |
link: function(scope, elem) { | |
var observer = new MutationObserver(function(mutations) { | |
try { | |
observer.disconnect(); | |
let e = mutations[0].target; | |
e.classList.forEach((c) => { | |
if (c.startsWith('ng-') || c.startsWith('text') || c.startsWith('pre-ep7-')) { | |
// skip | |
} else { | |
e.classList.remove(c); | |
e.classList.add('pre-ep7-' + c); | |
} | |
}); | |
console.log(e.classList); | |
} finally { | |
observer.observe(elem[0], { | |
attributes: true, | |
attributeFilter: ['class'] | |
}); | |
} | |
}); | |
observer.observe(elem[0], { | |
attributes: true, | |
attributeFilter: ['class'] | |
}); | |
elem.on('$destroy', function() { | |
observer.disconnect(); | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment