{"id":19937,"date":"2015-05-28T12:31:47","date_gmt":"2015-05-28T07:01:47","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=19937"},"modified":"2017-08-03T14:15:01","modified_gmt":"2017-08-03T08:45:01","slug":"directive-priority-in-angularjs","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/directive-priority-in-angularjs\/","title":{"rendered":"Directive Priority in AngularJS"},"content":{"rendered":"<p>Recently I discovered that every <a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-getting-started-with-directives\/\">directive in AngularJS<\/a> has a priority score assigned to it. While the page is loaded the priority plays a very important role in setting the order of the execution of various directives in single DOM Element. The priority is used to sort the directives before their &#8220;compile&#8221; function gets called. In simple words, the execution of directives is done in order of its priority score. Let us understand this using an example:<\/p>\n<p>[javascript]<br \/>\n\/\/Directive Definition<br \/>\nangular.module(&#8216;customDirectives&#8217;, [])<br \/>\n    .directive(&#8216;directive1&#8217;, function () {<br \/>\n        return {<br \/>\n            restrict: &quot;A&quot;,<br \/>\n            priority: 100,<br \/>\n            compile: function (element, attributes) {<br \/>\n                console.log(&quot;This is directive with priority 100.&quot;)<br \/>\n            }<br \/>\n        }<br \/>\n    }).directive(&#8216;directive2&#8217;, function () {<br \/>\n        return {<br \/>\n            restrict: &quot;A&quot;,<br \/>\n            priority: 200,<br \/>\n            compile: function (element, attributes) {<br \/>\n                console.log(&quot;This is directive with priority 200.&quot;)<br \/>\n            }<br \/>\n        }<br \/>\n    });<br \/>\n[\/javascript]<\/p>\n<p>[html]<br \/>\n&lt;p directive1=&quot;&quot; directive2=&quot;&quot;&gt;&lt;\/p&gt;<br \/>\n[\/html]<\/p>\n<p>Now if we use the directives defined on the top with above HTML we get following output in console.<\/p>\n<pre style=\"font: inherit;\">This is directive with priority 200.\r\nThis is directive with priority 100.\r\n<\/pre>\n<p>You can set the priority of custom directive by defining the &#8220;priority&#8221; attribute of the directive object (as shown in the example above) and when the priority is left undefined then it is set to the zero by default. Also, in case of same priority the directives are executed in the alphabetical order.<\/p>\n<p>For knowing the priority score of predefined directives please refer to their angular documentation.<\/p>\n<p>Hope you find this blog useful.<\/p>\n<p><span style=\"font-size: 20px; color: blue;\">Read Further on AngularJS Series<\/span><\/p>\n<ul>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-a-write-less-do-more-javascript-framework\/\">AngularJS :\u00a0A \u201cWrite Less Do More\u201d JavaScript Framework<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-updating-a-label-dynamically-with-user-input\/\">AngularJS :\u00a0Updating a Label Dynamically with User Input<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-adding-items-to-a-javascript-list-and-updating-corresponding-dom-dynamically\/\">AngularJS :\u00a0Adding Items to a JavaScript List and updating corresponding DOM Dynamically<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-text-suggestions\/\">AngularJS :\u00a0Text Suggestions<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-sorting-objects-on-various-attributes\/\">AngularJS :\u00a0Sorting objects on various attributes<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-fetching-data-from-the-server\/\">AngularJS :\u00a0Fetching data from the server<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-multiple-views-layout-template-and-routing\/\">AngularJS :\u00a0Multiple Views, Layout\u00a0Template\u00a0and Routing<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-implementing-routes-and-multiple-views\/\">AngularJS :\u00a0Implementing Routes And Multiple Views<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/building-intuitive-frontend-interfaces-with-angularjs\/\">Building Intuitive Frontend Interfaces with AngularJS<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Recently I discovered that every directive in AngularJS has a priority score assigned to it. While the page is loaded the priority plays a very important role in setting the order of the execution of various directives in single DOM Element. The priority is used to sort the directives before their &#8220;compile&#8221; function gets called. [&hellip;]<\/p>\n","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[1439],"tags":[3955,955,4697,1155,1815],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/19937"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=19937"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/19937\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=19937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=19937"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=19937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}