app.js
is:
var app = angular.module('myApp',[]);
app.directive('myDirective2', function () {
return{
restrict: 'A',
priority: 100,
//template:"<h1>myDirective2</h1>",
controller: function ($scope, $element, $transclude,$timeout) {
//$scope.name = "executed myDirective2";
$timeout(function () {
$scope.name = "executed myDirective2";
}, 3000);
}
};
});
app.directive('myDirective3', function () {
return{
restrict: 'A',
priority: 200,
//template:"<h1>myDirective3</h1>",
controller: function ($scope, $element, $transclude, $timeout) {
$timeout(function () {
$scope.name = "executed myDirective3";
}, 3000);
}
};
});
And index.html
is:
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script src="js/angular.js" type="text/javascript"></script>
<script src="js/app.js" type="text/javascript"></script>
</head>
<body>
<div my-directive3 my-directive2></div>
<br/>
Name:{{name}}
</body>
</html>
Though priority for my-directive2
is lesser than my-directive3
still why my-directive2
is getting executed? Should not it be the directive with higher priority which in this case is my-directive3
?
AngularJS directives are extended HTML attributes with the prefix ng- . The ng-app directive initializes an AngularJS application. The ng-init directive initializes application data. The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.
Directives with greater numerical priority are compiled first. Pre-link functions are also run in priority order, but post-link functions are run in reverse order. The order of directives with the same priority is undefined. The default priority is 0 .
1 Answer. Show activity on this post. Priority is a number for which directive gets executed first in case of multiple priorities. Basically you use it to determine the order of execution, not to exclude other directives.
Directives are markers on the DOM element which tell AngularJS to attach a specified behavior to that DOM element or even transform the DOM element with its children. Simple AngularJS allows extending HTML with new attributes called Directives.
Priority is a number for which directive gets executed first in case of multiple priorities. Basically you use it to determine the order of execution, not to exclude other directives.
Directives with greater numerical priority are compiled first. Pre-link functions are also run in priority order, but post-link functions are run in reverse order. The order of directives with the same priority is undefined. The default priority is 0.
You can read more about it here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With