Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding how directive priority level work in Angularjs

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?

like image 601
user4904589 Avatar asked Oct 16 '15 22:10

user4904589


People also ask

How does directive work in AngularJS?

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.

What is the priority level of directives in Angular?

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 .

What is priority level of directives?

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.

What is directive in AngularJS what is its importance Explain using simple example?

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.


1 Answers

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.

like image 126
arg20 Avatar answered Oct 18 '22 09:10

arg20