为什么使用 terminal: true 而不是删除较低优先级的指令? [英] Why use terminal: true instead of removing lower priority directives?

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

问题描述

相关:如何理解指令的`终端`?

为什么有人会在指令上设置 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 和终端:true 到指令 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>

在某些情况下,它可能通过允许将多个指令添加到元素并卸载决定哪些指令实际应用于 Angular 的工作来简化代码?

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-repeat 作为优先级 + 终端 + 嵌入的主要例子,所以我会给一个极其简化的 ng-repeat 版本:

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>

现在可以将额外的指令附加到包含假重复的同一个 li 上,但它们的优先级 + 终端选项将决定谁首先被编译,以及何时发生链接.通常我们希望 li 元素被克隆,并为 add-letter 指令复制每个绑定 t,但这将仅在 add-letter 时发生比 fake-repeat 的优先级低.

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.

为每个生成的 li 执行链接.

Linking is executed for each li generated.

链接在 fake-repeat 之前执行,因此在 transclude 发生之前.

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

编译在 fake-repeat 之前停止,因此指令永远不会执行.

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

这是一个带有控制台日志记录的 plunker 以供进一步探索.

Here is a plunker with console logging for further exploration.

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

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