简体   繁体   English

Angular中的指令优先级不起作用

[英]Directive priority in Angular not working

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) (我知道0是默认值)

dlEditor always gets executed first, then aceEditor . dlEditor总是首先被执行,然后是aceEditor What am I doing wrong? 我究竟做错了什么?

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. 当在单个DOM元素上定义了多个指令时,有时需要指定应用指令的顺序。 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. 默认优先级为0。

So, the directive with the higher priority ( aceEditor ) is compiled first, but it's post-link function (which seems to be the one of interest to you) is run last. 因此,首先编译具有较高优先级的指令( aceEditor ),但它的后连接功能(这似乎是您感兴趣的)最后运行。

You should either move the logic in the pre-link function (if that is applicable in your case) or reverse the priorities. 您应该在预链接功能中移动逻辑(如果这适用于您的情况)或反转优先级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM