加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在AngularJS中添加指令的指令

发布时间:2020-12-17 09:25:34 所属栏目:安全 来源:网络整理
导读:我试图构建一个指令,它需要添加更多的指令到它所宣告的元素。 例如,我想构建一个指令,该指令负责添加datepicker,datepicker-language和ng-required =“true”。 如果我试图添加这些属性,然后使用$ compile我显然生成一个无限循环,所以我检查如果我已经
我试图构建一个指令,它需要添加更多的指令到它所宣告的元素。
例如,我想构建一个指令,该指令负责添加datepicker,datepicker-language和ng-required =“true”。

如果我试图添加这些属性,然后使用$ compile我显然生成一个无限循环,所以我检查如果我已经添加了所需的属性:

angular.module('app')
  .directive('superDirective',function ($compile,$injector) {
    return {
      restrict: 'A',replace: true,link: function compile(scope,element,attrs) {
        if (element.attr('datepicker')) { // check
          return;
        }
        element.attr('datepicker','someValue');
        element.attr('datepicker-language','en');
        // some more
        $compile(element)(scope);
      }
    };
  });

当然,如果我不$编译元素,属性将被设置,但指令不会被引导。

这种方法是正确的,还是我做错了?有没有更好的方法来实现相同的行为?

UDPATE:由于$ compile是实现这个的唯一方法,有没有办法跳过第一个编译pass(该元素可能包含几个孩子)?也许通过设置terminal:true?

更新2:我试图把指令放入一个select元素,如所期望的,编译运行两次,这意味着有两倍的期望选项的数量。

在单个DOM元素上有多个指令的情况下
您可以使用priority属性对它们进行排序
应用。较高的数字首先运行。如果不指定一个,默认优先级为0。

编辑:讨论之后,这里是完整的工作解决方案。关键是删除属性:element.removeAttr(“common-things”);以及element.removeAttr(“data-common-things”); (如果用户在html中指定数据常见的东西)

angular.module('app')
  .directive('commonThings',function ($compile) {
    return {
      restrict: 'A',replace: false,terminal: true,//this setting is important,see explanation below
      priority: 1000,see explanation below
      compile: function compile(element,attrs) {
        element.attr('tooltip','{{dt()}}');
        element.attr('tooltip-placement','bottom');
        element.removeAttr("common-things"); //remove the attribute to avoid indefinite loop
        element.removeAttr("data-common-things"); //also remove the same attribute with data- prefix in case users specify data-common-things in the html

        return {
          pre: function preLink(scope,iElement,iAttrs,controller) {  },post: function postLink(scope,controller) {  
            $compile(iElement)(scope);
          }
        };
      }
    };
  });

工作喉咙可在:http://plnkr.co/edit/Q13bUt?p=preview

要么:

angular.module('app')
  .directive('commonThings',priority: 1000,link: function link(scope,'bottom');
        element.removeAttr("common-things"); //remove the attribute to avoid indefinite loop
        element.removeAttr("data-common-things"); //also remove the same attribute with data- prefix in case users specify data-common-things in the html

        $compile(element)(scope);
      }
    };
  });

DEMO

解释为什么我们要设置terminal:true和priority:1000(一个高的数字):

当DOM准备就绪时,angular遍历DOM以识别所有注册的指令,并且如果这些指令在同一元素上,则基于优先级逐个编译指令。我们将自定义指令的优先级设置为一个高数字,以确保它将首先被编译,并且使用terminal:true,其他指令将在编译完此指令后被跳过。

当编译自定义指令时,它将通过添加指令并删除自身来修改元素,并使用$ compile service来编译所有的指令(包括那些被跳过的指令)。

如果我们不设置terminal:true和priority:1000,有一些指令在我们的自定义指令之前编译。当我们的自定义指令使用$ compile编译element =>再次编译已编译的指令。这将导致不可预测的行为,特别是如果在我们的自定义指令之前编译的指令已经转换了DOM。

有关优先级和终端的更多信息,请查阅How to understand the `terminal` of directive?

还修改模板的指令的示例是ng-repeat(priority = 1000),编译ng-repeat时,ng-repeat在应用其他指令之前复制模板元素。

感谢@ Izhaki的评论,这里是参考ngRepeat源代码:https://github.com/angular/angular.js/blob/master/src/ng/directive/ngRepeat.js

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读