什么是你可以改变它NG重复指令`priority`? [英] What is `priority` of ng-repeat directive can you change it?

查看:200
本文介绍了什么是你可以改变它NG重复指令`priority`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

角文档说: -


  

在DOM的编制是由调用$编译(执行)
  方法。该方法遍历DOM和指令相匹配。如果一个
  找到匹配它被添加到指示与相关联的列表中
  给定的DOM元素。一旦给定的DOM元素的所有指令
  已经确定他们的按优先级并整理他们的
  编译()函数将被执行。


NG的重复指令,我相信比定制指令优先级较低,在某些情况下,使用像<一个href=\"http://stackoverflow.com/questions/19254705/index-of-ng-repeat-computed-after-linker-function-of-angular-directive-co\">dynamic ID和自定义指令。是否角许可的指令优先修修补补之前的其他选择一种执行?


解决方案

是的,你可以设置一个指令的优先级。 NG-重复为<一个优先href=\"https://github.com/angular/angular.js/blob/v1.2.0-rc.2/src/ng/directive/ngRepeat.js#L215\">1000,这实际上是不是自定义指令高(默认优先级为0)。您可以使用此号码为如何在相对于它设置在你的指令,你自己的优先级的指南。

  angular.module(X)指令(customPriority',函数(){
    返回{
        优先级:1001
        限制:'E',
        编译:功能(){
            返回功能(){...}
        }
    }
})


  

优先级 - 如果有一个单一的DOM元素上定义了多个指令,有时是必要的,以指定应用指令的顺序。优先级使用自己的编译功能被调用之前指令进行排序。优先级被定义为一个数字。以更大的数字优先级指令首先编译。指令具有相同优先级的顺序是不确定的。默认优先级为0。


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?

解决方案

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.

这篇关于什么是你可以改变它NG重复指令`priority`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆