Long before discovering Angular and jQuery, my go-to was good old plain Javascript like this:
function toggleClass(e, c) {
var classes = e.className.split(' ');
var match = false;
for(var i=0; i<classes.length; i++) {
if(classes[i] === c) {
match = true;
classes.splice(i,1);
break;
}
}
if(!match) classes.push(c);
e.className = classes.join(' ');
}
I've utilized this in the past to toggle a class name within an onclick
event like shown below:
<div onclick="toggleClass(this,'foo')"></div>
Check out this functional JSFiddle.
Now, how can I convert this into a directive in Angular?