Using AngularJS controllers created using angular.module (). Controller ()

I am still very new to AngularJS and am working on customizing my first application. I would like to be able to do the following:

angular.module('App.controllers', []) .controller('home', function () { $scope.property = true; }]); angular.module('App', ['App.controllers']) .config(['$routeProvider', function($routeProvider) { $routeProvider.when('/', {templateUrl: 'partials/home.html', controller: home}); }]); 

Using this setting, the following error is generated:

 Uncaught ReferenceError: home is not defined from App 

My question is: how can I register controllers using angular.module.controller() (or $controllerProvider.register() directly) and use the registered controller elsewhere in my application.

My motivation: I would like to avoid using global constructor functions as my controllers (like most examples of using angularjs.org) or a complex namespace. If I can register and use controllers as the names of individual variables (which then do not fit in the global scope), that would be ideal.

+44
angularjs
Jun 26 '12 at 16:38
source share
2 answers

Try using a string identifier.

routeProvider.when('/', {templateUrl: 'partials/home.html', controller: 'home'});

When you use a literal, it looks for a variable called home , but in this case this does not exist.

+75
Jun 26 2018-12-12T00:
source share

If you get a controller error, you should not include the controller name in quotation marks.

or define your controller as follows

 function controllerName() { //your code here } 

refer to this: Uncaught ReferenceError: the controller is not defined in AngularJS

-one
Jul 24 '13 at 11:04 on
source share



All Articles