Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is `priority` of ng-repeat directive can you change it?

Angular Documentation says: -

The compilation of the DOM is performed by the call to the $compile() method. The method traverses the DOM and matches the directives. If a match is found it is added to the list of directives associated with the given DOM element. Once all directives for a given DOM element have been identified they are sorted by priority and their compile() functions are executed.

The ng-repeat directive I believe has a lower priority than custom directives, in certain use cases like dynamic id and custom directive. Does angular permit tinkering with priority of directives to choose execution of one before the other?

like image 712
Sangram Singh Avatar asked Oct 09 '13 11:10

Sangram Singh


People also ask

What is the 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 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 .

Does ng-repeat create a new scope?

Directives that Create Scopes In most cases, directives and scopes interact but do not create new instances of scope. However, some directives, such as ng-controller and ng-repeat, create new child scopes and attach the child scope to the corresponding DOM element.

Which directive executes at priority level 1000?

Directive Info This directive creates new scope. This directive executes at priority level 1000.


2 Answers

Yes, you can set the priority of a directive. ng-repeat has a priority of 1000, which is actually higher than custom directives (default priority is 0). You can use this number as a guide for how to set your own priority on your directives in relation to it.

angular.module('x').directive('customPriority', function() {     return {         priority: 1001,         restrict: 'E',         compile: function () {             return function () {...}         }     } }) 

priority - When there are multiple directives defined on a single DOM element, sometimes it is necessary to specify the order in which the directives are applied. The priority is used to sort the directives before their compile functions get called. Priority is defined as a number. Directives with greater numerical priority are compiled first. The order of directives with the same priority is undefined. The default priority is 0.

like image 70
Cuong Vo Avatar answered Oct 20 '22 01:10

Cuong Vo


AngularJS finds all directives associated with an element and processes it. This option tells angular to sort directives by priority so a directive having higher priority will be compiled or linked before others. The reason for having this option is that we can perform conditional check on the output of the previous directive compiled.

In the followed example, first add button and only after add class to current button:

Demo Fiddle

App.directive('btn', function() {   return {     restrict: 'A',     priority: 1,     link: function(scope, element, attrs) {       element.addClass('btn');     }   }; });  App.directive('primary', function() {   return {     restrict: 'A',     priority: 0,     link: function(scope, element, attrs) {       if (element.hasClass('btn')) {         element.addClass('btn-primary');       }     }   }; }); 
like image 23
Maxim Shoustin Avatar answered Oct 19 '22 23:10

Maxim Shoustin