最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Directive priority in Angular not working - Stack Overflow

programmeradmin0浏览0评论

I have this element:

<div ace-editor dl-editor></div>

And these directives:

angular.module('DLApp')


.directive 'aceEditor', () ->
  restrict: 'A'
  priority: 10
  scope: false
  link: linkFunc1

.directive 'dlEditor', (Graph) ->
    restrict: 'A'
    priority: 0
    scope: false
    link: linkFunc2

(I'm aware that 0 is the default)

dlEditor always gets executed first, then aceEditor. What am I doing wrong?

I have this element:

<div ace-editor dl-editor></div>

And these directives:

angular.module('DLApp')


.directive 'aceEditor', () ->
  restrict: 'A'
  priority: 10
  scope: false
  link: linkFunc1

.directive 'dlEditor', (Graph) ->
    restrict: 'A'
    priority: 0
    scope: false
    link: linkFunc2

(I'm aware that 0 is the default)

dlEditor always gets executed first, then aceEditor. What am I doing wrong?

Share Improve this question asked Mar 13, 2014 at 19:35 MoshoMosho 7,0783 gold badges35 silver badges52 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 12

According to the docs:
emphasis mine

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 pile functions get called. Priority is defined as a number. Directives with greater numerical priority are piled 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.

So, the directive with the higher priority (aceEditor) is piled first, but it's post-link function (which seems to be the one of interest to you) is run last.

You should either move the logic in the pre-link function (if that is applicable in your case) or reverse the priorities.

发布评论

评论列表(0)

  1. 暂无评论