1

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.

6
  • Were all files app.js, DashboardController.js and UsersController.js were loaded in the order as described? Commented Jul 9, 2016 at 22:15
  • 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. Commented Jul 9, 2016 at 22:15
  • 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? Commented Jul 9, 2016 at 22:19
  • 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? Commented Jul 9, 2016 at 22:22
  • @SrinivasPaila that was my problem. I didn't call the file in my index.html file. Thank you so much! Commented Jul 9, 2016 at 22:24

1 Answer 1

-1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.