89

What's going on here?

Here is my directive:

app.directive('submitRequired', function (objSvc) {
    return {
        require: 'ngModel',
        link: function (scope, elm, attrs, ctrl) {

          // do something
        }
    };
});

Here is an example of the directive in use:

<input submit-required="true"></input>

Here is the actual error text:

Error: [$compile:ctreq] Controller 'ngModel', required by directive 'submitRequired', can't be found!
http://errors.angularjs.org/1.2.2/$compile/ctreq?p0=ngModel&p1=submitRequired
    at http://www.domain.ca/Scripts/angular/angular.js:78:12
    at getControllers (http://www.domain.ca/Scripts/angular/angular.js:5972:19)
    at nodeLinkFn (http://www.domain.ca/Scripts/angular/angular.js:6139:35)
    at compositeLinkFn (http://www.domain.ca/Scripts/angular/angular.js:5550:15)
    at nodeLinkFn (http://www.domain.ca/Scripts/angular/angular.js:6132:24)
    at compositeLinkFn (http://www.domain.ca/Scripts/angular/angular.js:5550:15)
    at publicLinkFn (http://www.domain.ca/Scripts/angular/angular.js:5458:30)
    at http://www.domain.ca/Scripts/angular/angular.js:1299:27
    at Scope.$get.Scope.$eval (http://www.domain.ca/Scripts/angular/angular.js:11634:28)
    at Scope.$get.Scope.$apply (http://www.domain.ca/Scripts/angular/angular.js:11734:23) <input submit-required="true"> angular.js:9159
(anonymous function) angular.js:9159
$get angular.js:6751
nodeLinkFn angular.js:6141
compositeLinkFn angular.js:5550
nodeLinkFn angular.js:6132
compositeLinkFn angular.js:5550
publicLinkFn angular.js:5458
(anonymous function) angular.js:1299
$get.Scope.$eval angular.js:11634
$get.Scope.$apply angular.js:11734
(anonymous function) angular.js:1297
invoke angular.js:3633
doBootstrap angular.js:1295
bootstrap angular.js:1309
angularInit angular.js:1258
(anonymous function) angular.js:20210
trigger angular.js:2315
(anonymous function) angular.js:2579
forEach angular.js:300
eventHandler angular.js:2578ar.js:7874
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
  • Does this answer your question? [Controller 'ngModel', required by directive '…', can't be found](https://stackoverflow.com/questions/21807929/controller-ngmodel-required-by-directive-cant-be-found) – shA.t Nov 15 '22 at 18:14

5 Answers5

130

As described here: Angular NgModelController, you should provide the <input with the required controller ngModel

<input submit-required="true" ng-model="user.Name"></input>
Danilo Cândido
  • 408
  • 1
  • 5
  • 18
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • 1
    Perfect. I appreciate that! I will mark this as answer. I have a follow up question. Should I post another question or change my original? – Shaun Luttin Feb 16 '14 at 05:56
  • Here is the follow up: http://stackoverflow.com/questions/21807929/controller-ngmodel-required-by-directive-cant-be-found – Shaun Luttin Feb 16 '14 at 06:04
  • 2
    i had mistakenly typed `ng-models` and got this error. – chovy Feb 11 '15 at 06:32
  • @Radim Kohler I'm happy that your response actually helped someone in need,i myself am close to achieving this. From the above input statement , for the "ng-model" attribute can i use string concatenation like "{{ RootObjectName+"."+ ModelName }}" ??!! As my models in the $scope are not straightforward and are dynamically created in the controller based on input from DB – pavan kumar Aug 24 '18 at 10:17
  • @pavankumar check this link http://next.plnkr.co/edit/iLP1QZjMJ7EkLlCF?open=lib%2Fscript.js&deferRun=1 this `ng-model="RootObject[alias]"` will work if session will have ` $scope.RootObject = {}; $scope.alias = "FirstName"` ... instaead of alias, even **forEach** could provide the dynamic name – Radim Köhler Aug 24 '18 at 10:33
  • @Radim Köhler thanks for the quick response Radim,let me just check it out – pavan kumar Aug 24 '18 at 10:35
  • @Radim Köhler i think in the plnkr the value in the scope variable is passed,what i want is to pass the model to the directive to be bound to a input element in the directive,is there a way i could do that?? – pavan kumar Aug 24 '18 at 10:46
  • @pavankumar, look, I tried to quickly help... but it seems you need more attention. Please, create new question, ask here... you will get your answer... comments are .. not the right place, I'd say.. good luck ;) – Radim Köhler Aug 24 '18 at 10:48
  • @Radim Köhler i actualy did, https://stackoverflow.com/questions/51966080/angular-6-custom-directive-with-ng-model – pavan kumar Aug 24 '18 at 10:50
  • The link in the answer doesn't work anymore – elki42 Aug 05 '21 at 07:41
8

One possible solution to this issue is ng-model attribute is required to use that directive.

Hence adding in the 'ng-model' attribute can resolve the issue.

<input submit-required="true" ng-model="user.Name"></input>
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
Ram
  • 15,908
  • 4
  • 48
  • 41
  • This solved mine. Thanks. I think we have missed the point that to trigger ng-change, there should be a ng-model bind to the element. – Anton Perera Jan 01 '20 at 09:34
1

You can also remove the line

  require: 'ngModel',

if you don't need ngModel in this directive. Removing ngModel will allow you to make a directive without thatngModel error.

steampowered
  • 11,809
  • 12
  • 78
  • 98
0

Issue is also caused by having ng-value instead of ng-model

Error:

<input ng-value="item.name" ng-change="nameChanged()"> 

Works:

<input ng-model="item.name" ng-change="nameChanged()">
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
user1865292
  • 11
  • 1
  • 3
-1

I faced the same error, in my case I miss-spelled ng-model directive something like "ng-moel"

Wrong one: ng-moel="user.name" Right one: ng-model="user.name"

enter image description here

Shahidul Haque
  • 99
  • 1
  • 11