ng-repeat 指令的“优先级"是什么,你能改变它吗? [英] What is `priority` of ng-repeat directive can you change it?

查看:24
本文介绍了ng-repeat 指令的“优先级"是什么,你能改变它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Angular 文档说:-

<块引用>

DOM 的编译是通过调用 $compile() 来执行的方法.该方法遍历 DOM 并匹配指令.如果一个找到匹配项,将其添加到与相关联的指令列表中给定的 DOM 元素.一旦给定 DOM 元素的所有指令已确定它们按优先级排序,并且它们的compile() 函数被执行.

我认为 ng-repeat 指令的优先级低于自定义指令,在某些用例中,例如 动态 ID 和自定义指令.angular 是否允许修改指令的优先级以在另一个之前选择执行?

解决方案

是的,您可以设置指令的优先级.ng-repeat 的优先级为 1000,实际上高于自定义指令(默认优先级为0).您可以使用此数字作为指南,了解如何在相关指令上设置您自己的优先级.

angular.module('x').directive('customPriority', function() {返回 {优先级:1001,限制:'E',编译:函数(){返回函数 () {...}}}})

<块引用>

priority - 当在单个 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-repeat 指令的“优先级"是什么,你能改变它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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