Without looking too deep into why, it is a fact that attrs
does not handle the class
attribute as expected.
You can watch the elements className
instead:
scope.$watch(function () {
return elem[0].className;
}, function (newValue, oldValue) {
if (newValue === oldValue) { return; }
alert(newValue);
});
See, also, this short demo.