0

why require option does not throw error in my directive when i use ngModel as require option

The below script is working

angular.module('myApp').directive('validNumber',[function(){
return{
    require:'ngModel',
    scope:{},
    link: function (scope ,element, attr, ngModel) {
     
    }
}}])      //here is my html <input ng-model="data" valid-number>

the below script is not working why? it throws compile error Missing Required Controller

angular.module('myApp').directive('validNumber',[function(){
return{
    require:'ngSandy',
    scope:{},
    link: function (scope ,element, attr, ngModel) {
     
    }
}}]) //here is my html <input ng-sandy="data" valid-number>

 
Community
  • 1
  • 1
Sandy
  • 309
  • 4
  • 13
  • So are you saying that you're not getting an error when there isn't an `ng-model` on the element the directive is on? e.g. `` should throw an error because no ng-model – Protozoid May 29 '18 at 14:43
  • i have this in my html.since it is working i dont know how it is working when there is no controller. require option will search for controller of same directive right? if we havent specified '?ngModel'. – Sandy May 29 '18 at 14:54
  • i updated my previous comment.please check @Protozoid – Sandy May 29 '18 at 14:57
  • From what I understand, `require: 'ngModel'` will return the controller of the ngModel directive, because directives are intended to be used as APIs for controlling the DOM. The answers [here](https://stackoverflow.com/questions/20930592/whats-the-meaning-of-require-ngmodel) and [here](https://stackoverflow.com/questions/14915332/what-does-require-of-directive-definition-object-take) provide a lot of depth. – Protozoid May 29 '18 at 14:57
  • Can you try adding a `console.log(ngModel);` in your link function and experiment with both `` and ``to see what you get in ngModel? – Protozoid May 29 '18 at 14:58
  • it will throw an compile error if we dont use ng-model in html element atrribute – Sandy May 29 '18 at 15:04
  • Well, then it sounds like it does exactly what it suggests ... when instantiating the `validNumber` directive, it is **required** to have the `ngModel` directive present on that element. – Protozoid May 29 '18 at 15:06
  • For reference, this is the controller you'd expect to get when requiring ngModel https://github.com/angular/angular.js/blob/master/src/ng/directive/ngModel.js#LC792 – Protozoid May 29 '18 at 15:11
  • Thanks @Protozoid i will check. – Sandy May 29 '18 at 15:23
  • Show us the code for the `ng-sandy` directive. Likely it is missing a controller. For more information, see [AngularJS Error Reference - $compile:ctreq](https://docs.angularjs.org/error/$compile/ctreq). – georgeawg May 29 '18 at 18:53
  • Avoid using the `ng-` as a prefix for custom directives. It is reserved for core AngularJS directives. For more information, see [AngularJS Wiki - Best Practices](https://github.com/angular/angular.js/wiki/Dev-Guide%3A-Best-Practices). – georgeawg May 29 '18 at 18:59

0 Answers0