Skip to content Skip to sidebar Skip to footer

Directive : $observe, Class Attribute Change Caught Only Once

I have a directive changing the style of a div and would like to be notified by $observe each time the class of the element changes. The problem is that it happens at directive cre

Solution 1:

$observe is used to observe the value changes of attributes that contain interpolation.

For example:

<spanclass="{{something}}">Hello</span>

In your example you can use $watch instead to watch for changes on the class attribute like this:

scope.$watch(function() {
  return element.attr('class');
}, function(newValue, oldValue) {
  if (newValue !== oldValue) { // Values will be equal on initializationconsole.log('class has changed to: ' + newValue);
  }
});

Working example: http://plnkr.co/edit/SImhYTkN11eAECC8gDXm?p=preview

Post a Comment for "Directive : $observe, Class Attribute Change Caught Only Once"