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

前端 未结 2 1830
你的背包
你的背包 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条回答
  •  南方客
    南方客 (楼主)
    2021-02-09 21:35

    I don't think $compile(), a link function, or terminal are necessary. Angular will automatically compile the telement for us.

    .directive('tag', [function() {
      return {
        priority: 1000,
        compile: function(telement, attrs) {
          attrs.$set('tag', null);
          attrs.$set('ngMaxlength', '10');
          attrs.$set('ngPattern', '/[\\w\\d]+/');
        }
      };
    }]);
    

    Tested with this HTML:

    
    {{test}}
    

    Plunker.

提交回复
热议问题