{"id":1796,"date":"2022-08-30T15:19:31","date_gmt":"2022-08-30T15:19:31","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/02\/angularjs-error-no-controller-ngmodel-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:19:31","modified_gmt":"2022-08-30T15:19:31","slug":"angularjs-error-no-controller-ngmodel-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/angularjs-error-no-controller-ngmodel-collection-of-common-programming-errors\/","title":{"rendered":"angularjs Error: No controller: ngModel-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;ve been working with AngularJS for a few days now, and I&#8217;m declaring two directives which work if I don&#8217;t require anything. But I need the <code>ngModel<\/code> to use the <code>$setViewValue<\/code> method.<\/p>\n<p>My directives look like this :<\/p>\n<pre><code>\n    \n\n<\/code><\/pre>\n<p>The directives are defined like :<\/p>\n<pre><code>var Application = Application || {};\n\n;(function(window, document, undefined) {\n\n    \"use strict\";\n\n    var CorinaJSDialog = {};\n\n    Application.CorinaJSDialog = angular.module(\"CorinaJSDialog\", [\"ngSanitize\"]);\n\n    Application.CorinaJSDialog.constant(\"CorinaJSAPI\", {\n\n        document : {\n            load : \"\/corina\/api\/loaddocument\",\n            save : \"\/corina\/api\/savedocument\"\n        }\n    });    \n\n    Application.CorinaJSDialog.directive(\"corina\", [\"$timeout\", function($timeout){\n\n        var object = {\n\n            restrict : \"E\",\n            require : \"ngModel\",\n\n            transclude : true,\n            replace : true,\n            priority : 1,\n            templateUrl : \"corina.html\",\n            controller : function($scope) {},\n            link : function(scope, element, attrs, controller) { console.log(controller); }\n        };\n\n        return object;\n    }]);\n\n    Application.CorinaJSDialog.directive(\"properties\", [\"$timeout\", function($timeout){\n\n        var object = {\n\n            restrict : \"E\",\n            require : \"?corina\",\n            transclude : true,\n            replace : true,\n            priority : 2,\n            terminal : true,\n            templateUrl : \"properties.html\",\n            controller : function($scope) {},\n            link : function(scope, element, attrs, controller) {\n\n                var loadXeditable = function() {\n\n                    $(element).editable({\n                        mode: \"inline\",\n                        display: function(value) {\n\n                            controller.$setViewValue(value);\n\n                            scope.$apply();\n\n                            $(this).html(value);\n                        }\n                    });\n                };\n\n                $timeout(function() {\n                    loadXeditable();\n                }, 10);\n\n            }\n        };\n\n        return object;\n    }]);\n\n})(window, document);\n<\/code><\/pre>\n<p>And the templates that are loaded :<\/p>\n<p><code>corina.html<\/code> :<\/p>\n<pre><code>\n    \n\n<\/code><\/pre>\n<p><code>properties.html<\/code> :<\/p>\n<pre><code>\n    \n        {{property.Name}}\n\n        \n            \n                \n            \n        \n    \n\n<\/code><\/pre>\n<p>Each time I run the above I get this error :<\/p>\n<pre><code>Error: No controller: ngModel\nat Error ()\nat i (https:\/\/ajax.googleapis.com\/ajax\/libs\/angularjs\/1.1.3\/angular.min.js:41:165)\nat l (https:\/\/ajax.googleapis.com\/ajax\/libs\/angularjs\/1.1.3\/angular.min.js:43:252)\nat https:\/\/ajax.googleapis.com\/ajax\/libs\/angularjs\/1.1.3\/angular.min.js:47:425\nat https:\/\/ajax.googleapis.com\/ajax\/libs\/angularjs\/1.1.3\/angular.min.js:96:330\nat h (https:\/\/ajax.googleapis.com\/ajax\/libs\/angularjs\/1.1.3\/angular.min.js:78:207)\nat https:\/\/ajax.googleapis.com\/ajax\/libs\/angularjs\/1.1.3\/angular.min.js:78:440\nat Object.e.$eval (https:\/\/ajax.googleapis.com\/ajax\/libs\/angularjs\/1.1.3\/angular.min.js:89:272)\nat Object.e.$digest (https:\/\/ajax.googleapis.com\/ajax\/libs\/angularjs\/1.1.3\/angular.min.js:87:124)\nat Object.e.$apply (https:\/\/ajax.googleapis.com\/ajax\/libs\/angularjs\/1.1.3\/angular.min.js:89:431)  angular.min.js:62\n<\/code><\/pre>\n<p>Also, if I require the directive like <code>^mydir<\/code>, I get the same error, or simply like <code>mydir<\/code>. Only if I use <code>?mydir<\/code> I get no error, but then I cannot use <code>ngModule<\/code> directive. I would appreciate some input on what is it that I&#8217;m doing wrong.<\/p>\n<ol>\n<li>\n<p>As I interpret it, you have two element directives, <code>corina<\/code> and <code>properties<\/code>, where <code>properties<\/code> wants the controller from <code>corina<\/code>. That will always fail because because they need to be on the same element for <code>require: 'corina'<\/code> to work. But I see now in your template that <code>properties<\/code> are inside of <code>corina<\/code>, so what you should do in <code>properties<\/code> is this: <code>require : \"^?corina\"<\/code> instead. That tells Angular to look for the corina controller in parent elements as well.<\/p>\n<p>Requiring <code>ngModel<\/code> in <code>corina<\/code> won&#8217;t work though, since <code>ng-model<\/code> isn&#8217;t set on the corina element. Your <code>ng-model<\/code> is set on an element inside the <code>corina<\/code> replacement template, so there&#8217;s no way for the <code>corina<\/code> directive to get that ngModelController. You can only ask for controllers from the same element or from parent elements, not from child elements. If you really need <code>ngModelController<\/code> you should add your own directive on the same element that has <code>ng-model<\/code> and require <code>ngModelController<\/code> there. That directive can require the <code>corina<\/code> controller (since the <code>corina<\/code> controller is on a parent element) and pass the <code>ngModelController<\/code> to the <code>corina<\/code> controller.<\/p>\n<p>But I&#8217;m not really sure why you want to use <code>ng-model<\/code> here in the first place? You&#8217;ve placed it on an anchor element which won&#8217;t do anything. What is the effect that your after of having <code>ng-model<\/code> on an anchor element?<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-02 01:34:29. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working with AngularJS for a few days now, and I&#8217;m declaring two directives which work if I don&#8217;t require anything. But I need the ngModel to use the $setViewValue method. My directives look like this : The directives are defined like : var Application = Application || {}; ;(function(window, document, undefined) { &#8220;use [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1796","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1796","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1796"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1796\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}