简体   繁体   中英

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.

如果所有文件都已被引用并加载到index.html或启动页面上,那么您应该没有任何问题

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM