directive that adds other directives to the same element in angular.js

前端 未结 2 1829
你的背包
你的背包 2021-02-09 21:08

How do I create a directive that adds other directives to an element?

For example, I want:


to be linked as:

2条回答
  •  -上瘾入骨i
    2021-02-09 21:40

    I came up with a solution that seems to work:

    .directive('tag', ['$compile', function($compile) {
      return {
        priority: 1000,
        terminal: true,
        compile: function(telement, attrs) {
          attrs.$set('tag', null);
          attrs.$set('ngMaxlength', '10');
          attrs.$set('ngPattern', '/[\\w\\d]+/');
    
          var link = $compile(telement);
    
          return function($scope, $element) {
            link($scope, function(clonedElement) {
             $element.replaceWith(clonedElement);
            });
          }
        }
      }
    }]);
    

    The key is making sure that the directive has a higher priority than all the other directives on the element and terminating, so that other directives arent compiled and linked.

提交回复
热议问题