Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
553 views
in Technique[技术] by (71.8m points)

angularjs - How to understand the `terminal` of directive?

In this page: http://docs.angularjs.org/guide/directive

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. directive1 with { priority: 1, terminal: false}
  2. directive2 with { priority: 10, terminal: false}
  3. directive3 with { priority: 100, terminal: false}
  4. directive4 with { priority: 100, terminal: true} // this is true
  5. directive5 with { priority: 1000, terminal: false}

Please note the directive4 has terminal:true and others have false.

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

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

What's the execution order of the 5 directives?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Priority

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.

Terminal

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>.

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");
        }
    }
});

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

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

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>

Original post

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...