Michael Myers
Michael Myers

Reputation: 143

AngularJS controller not a function, get undefined

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.

Upvotes: 1

Views: 227

Answers (1)

Srinivas Paila
Srinivas Paila

Reputation: 827

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

Upvotes: -1

Related Questions