require 
? 如果在当前指令中没有找到所需的控制器,就会将 null 传递给 link 函数中的第四个参数^ 如果添加了这个前缀,指令会在上游的指令链中查找 require 参数所指定的控制器?^ 合并了前面两个说法没有前缀 指令就会自身提供的控制器中查找,如果没有查找到,就会抛出异常来。 
 
link 对于 link 与 scope scope,我们需要用到一个 link 函数,他由指令定义对象中的 link 属性配置。
1 2 3 4 5 6 link:function (scope,elem,attrs )                     } 
所以说:link 函数主要是为 dom 元素添加事件监听,监听模型属性变化,以及更新 dom。require 的话,对于 link 函数方面就会多一个参数 controller
1 2 3 4 5 6 { require :'?ngModal' ,link:function (scope,element,attrs,ctrl )      } } 
如果 require 的参数是数组的话。
1 2 3 4 5 6 7 8 { require :['?ngModal' ,'?test' ],link:function (scope,element,attrs,ctrls )          var  modalCtrl = ctrls[0 ];     var  testCtrl = ctrls[1 ]; } } 
controller 类型:字符串或者函数
若是为字符串,则将字符串当做是控制器的名字,来查找注册在应用中的控制器的构造函数。 
 
1 2 3 4 5 6 7 8 9 10 angular.module('myApp' , []) .directive('myDirective' , function (   restrict: 'A' ,    controller: 'SomeController'  }) angular.module('myApp' ) .controller('SomeController' , function ($scope, $element, $attrs, $transclude )     }); 
若为匿名函数,直接在指令内部的定义为匿名函数,同样我们可以再这里注入任何服务($log,$timeout 等等) 
 
1 2 3 4 5 6 7 8 angular.module('myApp' ,[]) .directive('myDirective' , function (   restrict: 'A' ,   controller:     function ($scope, $element, $attrs, $transclude )             } }); 
另外还有一些特殊的服务(参数)可以注入:
$scope,与指令元素相关联的作用域$element,当前指令对应的 元素$attrs,由当前元素的属性组成的对象$transclude,嵌入链接函数,实际被执行用来克隆元素和操作 DOM 的函数$transclude。 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 angular.module('myApp' , []).directive('mySite' , function (   return  {     restrict: 'EA' ,     transclude: true ,      controller: function ($scope, $element, $attrs, $transclude, $log )               $transclude(function (clone )          var  a = angular.element('<a>' )         a.attr('href' , $attrs.site)         a.text(clone.text())         $element.append(a)       })       $log.info('hello everyone' )     }   } }) 
HTML 代码为:
1 <my-site  site ="http://www.cnblogs.com/cunjieliu" > 雷锋叔叔的博客</my-site > 
controller 函数与 require 如果你需要允许其他指令和你的指令发生交互的话,你需要使用 controller 函数,比如有些情况下,你需要通过组合两个指令来实现一个 ui 组件,那么你可以通过如下使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 app.directive('outerDirective' , function (   return  {     scope: {},     restrict: 'AE' ,     controller: function ($scope, $compile, $http )               this .addChild = function (nestedDirective )                   console .log(           'Got the message from nested directive:'  + nestedDirective.message         )       }     }   } }) 
当另外一个指令需要使用到这个 controller 的时候
1 2 3 4 5 6 7 8 9 10 11 12 13 14 app.directive('innerDirective' , function (   return  {     scope: {},     restrict: 'AE' ,     require : '^outerDirective' ,           link: function (scope, elem, attrs, controllerInstance )               scope.message = 'Hi, Parent directive'        controllerInstance.addChild(scope)     }   } })