Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
163
rated 0 times [  170] [ 7]  / answers: 1 / hits: 5914  / 10 Years ago, thu, june 5, 2014, 12:00:00

As I felt my single controller was growing too large I am now trying to make use of multiple controllers. However, my UserController can't be found for some reason when I navigate to /signup. I'm getting this error:



Error: [ng:areq] Argument 'UserController' is not a function, got undefined



app.js



var app = angular.module('myApp', [
'ui.router',
'ngResource',
'myApp.controllers',
]);

angular.module('myApp.controllers', []);

app.config(function($stateProvider, $urlRouterProvider, $httpProvider) {

$stateProvider
.state('signup', {
url: '/signup',
templateUrl: 'views/signup.html',
controller: UserController
});

});


I'm including the .js files in this order:



<script src=angular/controllers/mainCtrl.js></script> //binded to body tag
<script src=angular/controllers/userCtrl.js></script> //set in signup state
<script src=angular/app.js></script>


UserController



angular.module('myApp.controllers').controller('UserController', function () {

//do stuff

});


What am I missing?


More From » angularjs

 Answers
13

Make it easier on yourself and create cleaner code.



var app = angular.module('myApp', [
'ui.router',
'ngResource',
'myApp.controllers',
])
.config(function($stateProvider) {
$stateProvider
.state('signup', {
url: '/signup',
templateUrl: 'views/signup.html',
controller: UserController
});
});


you weren't using $urlRoutProvider and $httpProvider so why inject them?



Angular Modules are good for nothing...so far. Except for loading 3rd-party angular code into your app and mocking during testing. Other than that, there is no reason to use more than one module in your app.



To create your UserController do a



app.controller('UserController', function ($scope) {

//do stuff

});

[#44785] Thursday, June 5, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
austonjuancarlosb

Total Points: 238
Total Questions: 89
Total Answers: 99

Location: Chad
Member since Mon, Dec 5, 2022
1 Year ago
;