为什么使用终端:真正的而不是取消低优先级的指令? [英] Why use terminal: true instead of removing lower priority directives?

查看:128
本文介绍了为什么使用终端:真正的而不是取消低优先级的指令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关:如何理解指令的`terminal`

为什么会有人设置终端:true和优先级的指令,而不是简单地删除优先级较低的指令?例如,他们可以这样写:

Why would someone set terminal: true and a priority on a directive rather than simply removing the lower priority directives? For example, they could write:

<tag directive-1 directive-2 directive-3></tag>

...他们可以添加优先:100和终端:真到指令3,所以,只有指令-3将施加到元件

... and they could add priority: 100 and terminal: true to directive-3, so that only directive-3 would be applied to the element.

为什么会不会有人,而不是改变自己的模板:

Why wouldn't someone instead change their template to:

<tag directive-3></tag>

也许它通过允许多个指令被添加到一个元件和卸油决定哪些实际应用于角的工作简化在某些情况下code

Perhaps it simplifies the code in some cases by allowing multiple directives to be added to an element and offloading the work of deciding which ones to actually apply to Angular?

感谢。

推荐答案

设置优先级和终端选项是不是擦除指令,声明它的编译和链接的顺序。每个人都指向 NG-重复,所以我给NG重复的极度简化的版本:

Setting the priority and terminal options is not about erasing directives, it's declaring the order of compilation and linking. Everybody points to ng-repeat as the prime example of priority + terminal + transclude, so I'll give a extremely simplified version of ng-repeat:

app.directive('fakeRepeat', function($log) {
  return {
    priority: 1000,
    terminal: true,
    transclude: 'element',
    compile: function(el, attr, linker) {
      return function(scope, $element, $attr) {
        angular.forEach(scope.$eval($attr.fakeRepeat).reverse(), function(x) {
          var child = scope.$new();
          child[attr.binding] = x;
          linker(child, function(clone) {
            $element.after(clone);
          })
        })
      }
    }
  }
});

假重复指令可以被用作这样:

The fake repeat directive can be used as so:

<ul>
  <li fake-repeat="things" binding="t" add-letter>{{ t }}</li>
<ul>

现在额外的指令可以附加到包含假重复同样的礼,但它们的优先级+的终端选择将决定谁得到先编译和链接时发生。通常情况下,我们预计元素被复制,并为添加字母指令被复制每一个绑定 T ,但这只会发生,如果添加字母
 比假的重复一个较低的优先级。

Now extra directives can be attached to the same li that contains fake repeat, but their priority + terminal options will determine who gets compiled first, and when linking happens. Normally we expect the li element to be cloned and for and for the add-letter directive to be copied for each binding t, but that will only happen if add-letter has a lower priority than fake-repeat.

链接为每个执行生成的。

Linking is executed for each li generated.

链接是假的重复之前执行和transclude发生这样了。

Linking is executed before fake-repeat and thus before the transclude happens.

编制虚假重复这样永远不会执行该指令之前停止。

Compilation stops before fake-repeat so the directive is never executed.

下面是一个 plunker 随控制台记录作进一步的探索。

Here is a plunker with console logging for further exploration.

这篇关于为什么使用终端:真正的而不是取消低优先级的指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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