This is the ngStyle directive ngStyle :
var ngStyleDirective = ngDirective(function(scope, element, attr) { scope.$watch(attr.ngStyle, function ngStyleWatchAction(newStyles, oldStyles) { if (oldStyles && (newStyles !== oldStyles)) { forEach(oldStyles, function(val, style) { element.css(style, '');}); } if (newStyles) element.css(newStyles); }, true); });
Note that there is scope.$watch for attr.ngStyle , which is what causes it to run twice.
For example, if you try to do the same with ngInit , you will notice that the function is only called once. Now let's look at the ngInit directive ngInit , it looks like this:
var ngInitDirective = ngDirective({ priority: 450, compile: function() { return { pre: function(scope, element, attrs) { scope.$eval(attrs.ngInit); } }; } });
Please note that there is no watch in this directive.
source share