Skip to content

Instantly share code, notes, and snippets.

@sandipchitale
Last active July 28, 2019 19:51
Show Gist options
  • Save sandipchitale/63a089dcf0e267be6eb9cbc3df9d85bf to your computer and use it in GitHub Desktop.
Save sandipchitale/63a089dcf0e267be6eb9cbc3df9d85bf to your computer and use it in GitHub Desktop.
angularjs directive to monitor class attribute
.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