Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

controller not injected to link function when require used #11903

Closed
@vlio20

Description

@vlio20

I have this tag:

<input name="username" type="text" ng-model="vm.username" available>

and this directive:

  angular.module('friendlyBetsApp')
    .directive('available', RegistrationCtrl);

  function RegistrationCtrl() {
    var directive = {
      restrict: 'A',
      require: 'ngModel',
      link: linker,
      scope:true,
      controller: ExampleController,
      controllerAs: 'ctrl',
      bindToController: true
    };

    function linker(scope, elem, attrs, ngModel, ctrl) {
      ctrl.model = ngModel; //ctrl is undefined
      ctrl.setAsLoading(true);

    }

    return directive;
  }

  function ExampleController($scope) {

  }

  ExampleController.prototype.setAsLoading = function (bool) {
    this.model.$setValidity('loading', bool);
  }

Now if I remove require: ngModel the controller is injected to the linking function:

  angular.module('friendlyBetsApp')
    .directive('available', RegistrationCtrl);

  function RegistrationCtrl() {
    var directive = {
      restrict: 'A',
      //require: 'ngModel',
      link: linker,
      scope:true,
      controller: ExampleController,
      controllerAs: 'ctrl',
      bindToController: true
    };

    function linker(scope, elem, attrs,/* ngModel,*/ ctrl) {
      ctrl.model = ngModel; //ctrl is defined, and ngModel is undefined (daaa) 
      ctrl.setAsLoading(true);

    }

    return directive;
  }

...

Is it a bug?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions