user90766
user90766

Reputation: 329

Angular JS Missing Required Controller error: Controller 'ngModel', required by directive can't be found

I have this directive:

directive('myDirective',
    function() {
        return {
            restrict: 'EA',
            scope: {
                params: '=ngModel'
            },
            //template: '',
            templateUrl: '/myTemplate.html',
            controller: 'myController',
            link: function(scope, iElement, iAttrs, ngModel) {
               // code.. 
            }
        };
    }
);

However when I use this directive, I get the following error in the console: $compile:ctreq, with a hyperlink to the following message: Missing Required Controller error in component $compile Controller 'ngModel', required by directive 'myDirective', can't be found!

The error goes away if I use "template" instead of "templateUrl", and I do not wish to use the "template". This seems to be a known issue: https://github.com/angular/angular.js/issues/4603

Can anyone please suggest a workaround? Edit: I am using ngModel because I want 2-way binding

Upvotes: 1

Views: 4440

Answers (1)

DavidC
DavidC

Reputation: 1862

If you want to pass a parameter to a directive you can do it this way - what you have in your question does not look quite right to me.

<div my-directive my-param="foo"></div>


directive('myDirective',
    function() {
        return {
           restrict: 'EA',
            scope: {
                myParam: '='
            },
            //template: '',
            templateUrl: '/myTemplate.html',
            controller: 'myController',
            link: function(scope, iElement, iAttrs, ngModel) {
               scope.myParam...
            }
       };
   }

);

Upvotes: 0

Related Questions