0

When I came across angularJS API guide i happen to see AngularJS priority executes at 400. Why is this so specific and can we override this priority using "priority Attribute"?

<ng-include
  src="string"
  [onload="string"]
  [autoscroll="string"]>
</ng-include>
Karthick Radhakrishnan
  • 1,151
  • 3
  • 12
  • 32
  • 1
    What is your specific scenario that you need to change the priority? And I'm not sure what you mean by "so specific" - Angular doesn't support ranges of priority, so any number would have been "specific" – New Dev Aug 28 '15 at 06:18
  • Angular does supports priority, Please check- http://stackoverflow.com/questions/19270392/what-is-priority-of-ng-repeat-directive-can-you-change-it – Karthick Radhakrishnan Aug 28 '15 at 06:26
  • Yeah, I know... but any priority would thus be "specific" to some number – New Dev Aug 28 '15 at 06:27
  • Please check the API for ngInclude. the priority level is set to 400. Which is specific. I'm Just trying to understand. https://docs.angularjs.org/api/ng/directive/ngInclude – Karthick Radhakrishnan Aug 28 '15 at 06:30
  • Why don't you just tell us what your scenario is that requires you to change the priority? – New Dev Aug 28 '15 at 06:32
  • Why don't you tell me why the priority is set with this number.I told you i'm just trying to understand the API. – Karthick Radhakrishnan Aug 28 '15 at 06:35
  • Ok, ***any*** priority number would have caused to you to ask "why is this priority so specific". Every directive has a priority, so why not ask "why directive X has specific priority Y"... It's all relative to other built-in directives' priorities. It's just lower than `ngInit` for example, and also lower than `ngIf`, `ngSwitch` and `ngRepeat` – New Dev Aug 28 '15 at 06:45
  • "Angular doesn't support ranges of priority" in your first comment what is this mean?.I'm just here to get clarity and not for debate.Thank You – Karthick Radhakrishnan Aug 28 '15 at 06:51

1 Answers1

3

Because it's the level at which angular designers thought that their directives should be used.Now if your question is why priority is defined over angular directives than the reason is

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. 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.

See this for more information.

If you would like to see general aggregation list of angular core directive priorities than here it is. Also one of the good read on angular directive.

And "can we override this priority using "priority Attribute"?" : You can't override AngularJS built-in directives. However, you can define multiple directives with the same name and have them executed against the same element.By assigning appropriate priority to your directive, you can then control whether your directive runs before or after a built-in directive

Angular permit tinkering with priority of directives to choose execution of one before the other. For example 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 () {...}
    }
   }
  })

This plunker shows how to build an ng-click directive that executes before the built-in ng-click does.When clicking the link, the custom ng-click will run first, then the built-in ng-click does.

Community
  • 1
  • 1
road2victory
  • 486
  • 4
  • 12