AngularJS指令控制器和要求

时间:2015-01-05 12:04:32

标签: angularjs angularjs-directive angularjs-scope

如何在Link函数中获取ngModel和SelectBoxController的实例?

指令

angular
 .module('directives.selectBox', [])
 .directive('selectBox', selectBox);

  function selectBox() {
      return {
         restrict   : 'E',
         require    : ['ngModel'],
         scope      : {
           list     : '=',
         },
         replace     : true,
         templateUrl : 'common/directives/selectBox/selectBox.html',
         controller :  SelectBoxController,
         link: function(scope, element, attrs, controllers) {                 
            console.log(controllers);
         }
      };
  }

1 个答案:

答案 0 :(得分:1)

使用'要求'获得ngModelControllerSelectBoxController

  function selectBox() {
      return {
         restrict   : 'E',
         require    : ['ngModel','selectBox'],
         scope      : {
           list     : '=',
         },
         replace     : true,
         templateUrl : 'common/directives/selectBox/selectBox.html',
         controller :  SelectBoxController,
         link: function(scope, element, attrs, controllers) {                 
            console.log(controllers[0]);
            console.log(controllers[1]);
         }
      };
  }