1
votes

I've recently come into a strange problem with my Angular controller being undefined. It seems to not see the controller, even though another controller is working just fine (with all the same code).

Here is my app.js

var myApp = angular.module('myApp', ['ngRoute']);

myApp.config(function($routeProvider){
    $routeProvider
    .when('/',{
        controller: 'DashboardController',
        templateUrl: 'views/dashboard.html'
    })
    .when('/users',{
        controller: 'UsersController',
        templateUrl: 'views/users.html'
    })
    .otherwise({
        redirectTo: '/'
    });
});

my WORKING controller is my dashboard controller here

var myApp = angular.module("myApp");

myApp.controller('DashboardController', ['$scope', '$http', '$location', function($scope, $http, $location){
    console.log("dashboard init");
}]);

and the controller that isn't working is my User controller

var myApp = angular.module('myApp');

myApp.controller('UsersController', ['$scope', '$http', '$location', function($scope, $http, $location){
    console.log("users init");
}]);

This is the error I'm getting: angular.js:12450 Error: [ng:areq] Argument 'UsersController' is not a function, got undefined

I've even copied the Dashboard completely over to user and just changed the controller name to "UsersController" and still no luck.

1
Were all files app.js, DashboardController.js and UsersController.js were loaded in the order as described?Srinivas Paila
Did you include the UsersController file at the same time as your DashboardController? What you show seems like it would work, it would probably be best if you setup a jsfiddle to demonstrate the problem.monokh
yes, the files were loaded exactly like that and @Rubelet I'm not quite sure what you mean by including. I have both controller files in the same "controller" folder, and I'm calling them in my app.js file. Is there somewhere else it must be included?Michael Myers
If all the files were referenced and loaded on your index.html or a start-up page, then you should not have any issues. Can you set up a fiddle?Srinivas Paila
@SrinivasPaila that was my problem. I didn't call the file in my index.html file. Thank you so much!Michael Myers

1 Answers

-1
votes

If all the files were referenced and loaded on your index.html or a start-up page, then you should not have any issues