如何理解指令的`terminal`? [英] How to understand the `terminal` of directive?

查看:212
本文介绍了如何理解指令的`terminal`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在本页面: http://docs.angularjs.org/guide/directive

如果设置为true,则当前的优先级将是最后的指令集将执行(任何指示在当前优先权仍将执行的执行上相同的优先级顺序是不确定的)的。

Directive Definition Object

terminal

If set to true then the current priority will be the last set of directives which will execute (any directives at the current priority will still execute as the order of execution on same priority is undefined).

我不明白它做好。是什么当前优先是什么意思?如果有这样的指令:

I don't understand it well. What does current priority mean? If there are such directives:


  1. 指令1与{重点:1,终端:假}

  2. 指令2与{优先级:10,终端:假}

  3. directive3与{优先级:100,终端:假}

  4. directive4与{优先级:100,终端:真正} //这是真的

  5. directive5与{优先权:1000,终端:假}

请注意, directive4 端子:真正的等都有

如果有一个HTML标签具有所有5个指令:

If there is a html tag has all of the 5 directives:

<div directive1 directive2 directive3 directive4 directive5></div>

什么是5指令的执行顺序?

What's the execution order of the 5 directives?

推荐答案

优先级

当你有一个元素上有多个指令的优先级才有意义。以什么顺序这些指令将被应用的优先级决定/启动。在大多数情况下,你不会需要一个优先事项,但有时当你使用编译功能,你要确保你的编译功能,第一次运行。

The priority is only relevant when you have multiple directives on one element. The priority determines in what order those directives will be applied/started. In most cases you wouldn't need a priority, but sometimes when you use the compile function, you want to make sure that your compile function runs first.

终端

终端酒店也只对是相同的HTML元素的指令相关。也就是说,如果你有&LT; D​​IV我-指令1&GT;&LT; / DIV&GT; &LT; D​​IV我-指令2&GT;&LT; / DIV&GT; 优先级端子中您的指令我-指令1 我-指令2 不会互相影响。如果你有他们只会互相影响&LT; D​​IV我,我的指令1,指令2&GT;&LT; / DIV&GT;

The terminal property is also only relevant for directives that are on the same HTML element. That is, if you have <div my-directive1></div> <div my-directive2></div>, priority and terminal in your directives my-directive1 and my-directive2 won't affect each other. They will only affect each other if you have <div my-directive1 my-directive2></div>.

终端属性告诉角跳过后(低优先级)自带的元素上所有指令。所以这code可能它清除掉:

The terminal property tells Angular to skip all directives on that element that comes after it (lower priority). So this code might clear it up:

myModule.directive('myDirective1', function() {
    return {
        priority: 1,
        terminal: false,
        link: function() {
            console.log("I'm myDirective1");
        }
    }
});

myModule.directive('myDirective2', function() {
    return {
        priority: 10,
        terminal: true,
        link: function() {
            console.log("I'm myDirective2");
        }
    }
});

myModule.directive('myDirective3', function() {
    return {
        priority: 100,
        terminal: false,
        link: function() {
            console.log("I'm myDirective3");
        }
    }
});

对于这一点,你只看到我myDirective2和我myDirective3在控制台中。

For this, you'd only see "I'm myDirective2" and "I'm myDirective3" in the console.

<div my-directive1 my-directive2 my-directive3></div>

不过,对于这一点,你会看到我myDirective1为好,因为它们是不同的元素。

But for this, you'd see "I'm myDirective1" as well, since they are on different elements.

<div my-directive1></div>
<div my-directive2></div>
<div my-directive3></div>

原帖

在你的榜样,优先100和1000的指令是,将得到应用唯一的,因为具有较高优先级的指令被首先应用,所以优先1000人会首先应用。

In your example the directives with priority 100 and 1000 are the only ones that will get applied, since a directive with higher priority are applied first, so the one with priority 1000 will be applied first.

如果您在这种情况下有两个指令优先级100,他们都将被应用,因为指令具有相同的优先级顺序是不确定的。

If you have two directives with priority 100 in this case, both of them will be applied because the order of directives with the same priority is undefined.

请注意,这仅适用于那些在相同的元素指令。

Note that this only applies to directives that are on the same element.

这篇关于如何理解指令的`terminal`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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