while i was playing with ng-view and ng-animate from angularjs 1.1.4, i noticed that directives are executed twice. once for the element in the view that enters the screen and again for the element in the view which leaves the screen (while the directive was already executed for the element when the view entered the screen).
for my understanding, directives should only be executed for elements which enter the screen and not for the ones which leaves. or am i missing something?
<div ng-app="app">
<div ng-controller="AppCtrl">
<div class="btn-group">
<a class="btn btn-primary" href="./">foo</a>
<a class="btn btn-primary" href="bar/">bar</a>
</div>
<span class="btn btn-success">{{count}}</span>
<div class="view" ng-view ng-animate="{enter: 'view-enter', leave: 'view-leave'}"></div>
</div>
</div>
var app = angular.module('app', []);
app.config(function ($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
template: '<h1 color>foo</h1>'
})
.when('/bar', {
template: '<h1 color>bar</h1>'
})
.otherwise({
redirectTo: '/'
});
$locationProvider.html5Mode(true);
});
app.controller('AppCtrl', function ($scope) {
$scope.count = 0;
$scope.incrementCount = function () {
$scope.count++;
}
});
app.directive('color', function () {
return {
restrict: 'A',
link: function ($scope, $element, $attrs) {
// executed once the app inits but
// twice when you change the view
$scope.incrementCount();
}
}
});
i've setup a jsfiddle for this http://jsfiddle.net/mediastuttgart/f4FPj/1/
as you can see, the counter says 1 when the app inits. but if you start navigating, the counter increments by 2.
cheers michael
EDIT
of course a workaround could be to add a class to the element and check for it in the directive – but i guess that's not the way it should be accomplished.
link: function ($scope, $element, $attrs) {
if ($element.hasClass('processed')) return;
$element.addClass('processed');
$scope.incrementCount();
}