指令,增加了其他指令相同的元素angular.js [英] directive that adds other directives to the same element in angular.js

查看:110
本文介绍了指令,增加了其他指令相同的元素angular.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建一个指令,增加了其他指令到一个元素?

例如,我想:

 <输入标签/>

被链接为:

 <输入NG-模式='/ [\\\\ W \\\\ D]。+ /'NG-最大长度= '10'/>


解决方案

我不认为 $编译(),链接功能,或终端是必要的。角将自动编译 telement 我们。

  .directive('标签',[功能(){
  返回{
    优先级:1000,
    编译:功能(telement,ATTRS){
      ATTRS $集('标签',NULL);
      ATTRS $集('ngMaxlength','10')。
      ATTRS $集('ngPattern','/ [\\\\ W \\\\ D]。+ /')。
    }
  };
}]);

这个HTML测试:

 <输入NG模型=测试NG-的init =测试= 2的标签>
{{测试}}

Plunker

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

For example, I want:

<input tag/>

to be linked as:

<input ng-pattern='/[\\w\\d]+/' ng-maxlength='10'/>

解决方案

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:

<input ng-model="test" ng-init="test=2" tag>
{{test}}

Plunker.

这篇关于指令,增加了其他指令相同的元素angular.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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