{"id":2170,"date":"2022-08-30T15:22:38","date_gmt":"2022-08-30T15:22:38","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/04\/using-angular-controllers-created-with-angular-module-controller-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:22:38","modified_gmt":"2022-08-30T15:22:38","slug":"using-angular-controllers-created-with-angular-module-controller-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/using-angular-controllers-created-with-angular-module-controller-collection-of-common-programming-errors\/","title":{"rendered":"Using Angular Controllers created with angular.module().controller()-Collection of common programming errors"},"content":{"rendered":"<p>I am still very new to Angular.js and am working through setting up my first application. I would like to be able to do the following:<\/p>\n<pre><code>angular.module('App.controllers', [])\n  .controller('home', function () {\n    $scope.property = true;\n  }]);\n\nangular.module('App', ['App.controllers'])\n  .config(['$routeProvider', function($routeProvider) {\n    $routeProvider.when('\/', {templateUrl: 'partials\/home.html', controller: home});\n  }]);\n<\/code><\/pre>\n<p>Using this setup the following error is generated:<\/p>\n<pre><code>Uncaught ReferenceError: home is not defined from App\n<\/code><\/pre>\n<p>My question is: How can I register controllers using <code>angular.module.controller()<\/code> (or <code>$controllerProvider.register()<\/code> directly) and use the registered controller elsewhere in my app.<\/p>\n<p>My motivation: I would like to avoid using either global constructor functions as my controllers (as most of the examples on angularjs.org use) or complex namespacing. If I can register and use controllers as single variable names (that are not then put in the global scope) that would be ideal.<\/p>\n<ol>\n<li>\n<p>Try using a string identifier.<\/p>\n<p><code>routeProvider.when('\/', {templateUrl: 'partials\/home.html', controller: 'home'});<\/code><\/p>\n<p>When you use a literal, it is looking for a variable called <code>home<\/code>, but that doesn&#8217;t exist in this case.<\/p>\n<\/li>\n<li>\n<p>If you are getting controller is not defined error you have to write your controller name within the quotes.<\/p>\n<p>or define your controller like this<\/p>\n<pre><code>function controllerName()\n{\n   \/\/your code here\n}\n<\/code><\/pre>\n<p>refer this: post on this issue<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-01-04 02:50:52. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am still very new to Angular.js and am working through setting up my first application. I would like to be able to do the following: angular.module(&#8216;App.controllers&#8217;, []) .controller(&#8216;home&#8217;, function () { $scope.property = true; }]); angular.module(&#8216;App&#8217;, [&#8216;App.controllers&#8217;]) .config([&#8216;$routeProvider&#8217;, function($routeProvider) { $routeProvider.when(&#8216;\/&#8217;, {templateUrl: &#8216;partials\/home.html&#8217;, controller: home}); }]); Using this setup the following error is generated: [&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-2170","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2170","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=2170"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2170\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}