I'm quite new to AngularJS and have already found a lot of helpfull answers on this forum, however the following item is driving me nuts.
First of all, let me tell you what I want to do: I do have an element, of which I would like to change the color property at runtime, but only when that particular element has the class 'active'.
Therefore, I have created a directive which looks like the following:
OfficeUIRibbon.directive('officeActiveStyle', function () {
return {
restrict: 'A',
link: function (scope, element, attr) {
scope.$watch(function() {
return element.attr('class');
}, function(newValue, oldValue) {
if (newValue !== oldValue) { // Values will be equal on initialization
alert('Changed');
}
});
}
};
});
As far as my knowledge goes, this does watch the attribute class and the if check in their does make sure that the watch isn't triggered when the application is first launched.
Now, the class property of the element is being set by using the ng-click directive.
When I now execute the HTML, nothing happens. When I click the 'set active' link, the class is changed, but the directive doesn't show me an alert. (Why is that?). If I then change the class again, then the directive does show me an alert.
So, why, on the first click, the alert of the directive isn't triggered?
I've created a plunker for better understanding.
Hopefully, someone can tell me what's wrong with this because it's really blocking my work.
change colorso vague and now what seems like best approach isn't. Where do the color values come from and why aren't you focusing on them? This is a classic case of anX-Y problem