Miron
Miron

Reputation: 2137

AngularJS directive controller and require

How to get instances of ngModel and SelectBoxController in Link function?

Directive

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);
         }
      };
  }

Upvotes: 1

Views: 39

Answers (1)

Michael Kang
Michael Kang

Reputation: 52847

Use 'require' to get both the ngModelController and SelectBoxController:

  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]);
         }
      };
  }

Upvotes: 1

Related Questions