转自:https://www.cnblogs.com/best/tag/Angular/

关于自定义指令的命名,你可以随便怎么起名字都行,官方是推荐用[命名空间-指令名称]这样的方式,像ng-controller。不过你可千万不要用 ng-前缀了,防止与系统自带的指令重名。另外一个需知道的地方,指令命名时用驼峰规则,使用时用-分割各单词。如:定义myDirective,使用时 像这样:<my-directive>。

这里列出directive的合法命名:

  • ng:bind
  • ng-bind
  • ng_bind
  • x-ng-bind
  • data-ng-bind

我是教师,在新建试题输入分数的时候应该只能输入数字才对,输入其他内容是不合法的,而且我希望这个分数是1~10之间的数字。能否只在输入框上加一个属性.我们定义一个叫做fractionNum的指令如下

  1. app.directive('fractionNum',function(){
  2. return {
  3. link : function(scope, elements, attrs, controller){
  4. elements[0].onkeyup = function(){
  5. if(isNaN(this.value) || this.value<1 || this.value>10){
  6. this.style.borderColor = 'red';
  7. }
  8. else{
  9. this.style.borderColor = '';
  10. }
  11. };
  12. }
  13. };
  14. });

link的值是一个函数,用来定义指令的行为。从传入的参数中可以获取到当前元素,我们便可以拿当前元素开刀了。我在此处监听当前元素的keyup事件,获取元素的值,如果不是1~10之间的数字,则把输入框的边框颜色变为红色。这下这个指令就可以工作了。定义好的指令就可以在模板中使用了,使用方法如下:

  1. 分数:<input type="text" ng-model="question.fraction" fraction-num /><br />
 
 
controller,link,compile有什么不同

  1. //directives.js增加exampleDirective
  2. phonecatDirectives.directive('exampleDirective', function() {
  3. return {
  4. restrict: 'E',
  5. template: '<p>Hello {{number}}!</p>',
  6. controller: function($scope, $element){
  7. $scope.number = $scope.number + "22222 ";
  8. },
  9. //controllerAs:'myController',
  10. link: function(scope, el, attr) {
  11. scope.number = scope.number + "33333 ";
  12. },
  13. compile: function(element, attributes) {
  14. return {
  15. pre: function preLink(scope, element, attributes) {
  16. scope.number = scope.number + "44444 ";
  17. },
  18. post: function postLink(scope, element, attributes) {
  19. scope.number = scope.number + "55555 ";
  20. }
  21. };
  22. }
  23. }
  24. });
  25. //controller.js添加
  26. dtControllers.controller('directive2',['$scope',
  27. function($scope) {
  28. $scope.number = '1111 ';
  29. }
  30. ]);
  31. //html
  32. <body ng-app="phonecatApp">
  33. <div ng-controller="directive2">
  34. <example-directive></example-directive>
  35. </div>
  36. </body>

 运行结果:

  1. Hello 1111 22222 44444 55555 !

由结果可以看出来,controller先运行,compile后运行,link不运行(link就是compile中的postLink)。将上例中的compile注释掉运行结果:

  1. Hello 1111 22222 33333 !

由结果可以看出来,controller先运行,link后运行,link和compile不兼容。compile改变dom,link事件的触发和绑定

49.AngularJs 指令directive之controller,link,compile的更多相关文章

  1. 【转载】AngularJs 指令directive之controller,link,compile

    关于自定义指令的命名,你可以随便怎么起名字都行,官方是推荐用[命名空间-指令名称]这样的方式,像ng-controller.不过你可千万不要用 ng-前缀了,防止与系统自带的指令重名.另外一个需知道的 ...

  2. AngularJs 指令 directive中link,controller 的区别

    其实严格来讲,link和controller是完全不同的概念,这里讲区别有点牵强. angular指令中,带有link和controller两个函数,很多人在写指令的时候不知道是写在link里 还是c ...

  3. AngularJS 指令生命周期 complie link

    AnguarJS指令从解析到生效一共会经历Inject.Compile.Controller加载.Pre-link.Post-link这几个主要阶段. 一.AngularJS指令执行过程 1.加载An ...

  4. angularJS中directive与controller之间的通信

    当我们在angularJS中自定义了directive之后需要和controller进行通讯的时候,是怎么样进行通讯呢? 这里介绍3种angular自定义directive与controller通信的 ...

  5. AngularJS自定义Directive与controller的交互

    有时候,自定义的Directive中需要调用controller中的方法,即Directive与controller有一定的耦合度. 比如有如下的一个controller: app.controlle ...

  6. angularJs 指令调用父controller某个方法

    1.父级controller:例如有个 init() 方法; 父级与子级的通信数据是$scope.controlFlag={}; 那么可以在父级controller里这样写:$scope.contro ...

  7. AngularJs 指令directive之require

    controller的用法分为两种情形,一种是require自定义的controller,由于自定义controller中的属性方法都由自己编 写,使用起来比较简单:另一种方法则是require An ...

  8. Angularjs之directive指令学习笔记(二)

    1.Directive的五个实例知道driective作用.其中字段restrict.template. replace.transclude.link用法 参考文章链接地址:http://damoq ...

  9. AngularJS的指令(Directive) compile和link的区别及使用示例

    如果我想实现这样一个功能,当一个input失去光标焦点时(blur),执行一些语句,比如当输入用户名后,向后台发ajax请求查询用户名是否已经存在,好有及时的页面相应. 输入 camnpr 失去焦点后 ...

随机推荐

  1. ZOJ 3687

    赤裸的带禁区的排列数,不过,难点在于如何用程序来写这个公式了.纠结了好久没想到,看了看别人的博客,用了DFS,实在妙极,比自己最初想用枚举的笨方法高明许多啊.\ http://blog.csdn.ne ...

  2. [Gatsby] Install Gatsby and Scaffold a Blog

    In this lesson, you’ll install Gatsby and the plugins that give the default starter the ability to t ...

  3. sizeof()函数的使用——————【Badboy】

    1.sizeof的使用:sizeof操作符以字节形式给出了其操作数的存储大小. sizeof操作符不能用于函数类型,不全然类型或位字段.不全然类型指具有未知存储大小的数据类型,如未知存储大小的数组类型 ...

  4. storm-安装

            storm有两种操作模式: 本地模式和远程模式.使用本地模式的时候,你能够在你的本地机器上开发測试你的topology, 一切都在你的本地机器上模拟出来; 用远端模式的时候你提交的to ...

  5. hdu2546 饭卡 01-背包问题

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2546 Problem ...

  6. ICMP 隧道——将流量封装进 IMCP 的 ping 数据包中,旨在利用 ping 穿透防火墙的检测

    利用 ICMP 隧道穿透防火墙 转自:http://xiaix.me/li-yong-icmp-sui-dao-chuan-tou-fang-huo-qiang/ 以前穿透防火墙总是使用 SSH 隧道 ...

  7. 正睿NOIP赠送附加赛1

    T1:math 题目链接: http://zhengruioi.com/contest/156/problem/471 题解: 先讲讲我的乱搞做法.对于前面70%,我跑了背包.因为背包有后效性...我 ...

  8. DateForamt和SimpleDateFormat

    1.因为DateFormat是抽象类,所以只能用子类来初始化 DateFormat df = new SimpleDateFormat("yyyy--MM--dd HH:MM:ss,属于本年 ...

  9. Android VelocityTracker类和Scroller类

    VelocityTracker类:用于跟踪触屏事件的速度,通常使用VelocityTracker的步骤如下: static VelocityTracker obtain():获取一个VelocityT ...

  10. SpringCloud学习笔记(10)----Spring Cloud Netflix之声明式 REST客户端 -Feign的高级特性

    1. Feign的默认配置 Feign 的默认配置 Spring Cloud Netflix 提供的默认实现类:FeignClientsConfiguration 解码器:Decoder feignD ...