Uncaught Error:AngularJS中的[$ injector:modulerr]

时间:2015-07-07 16:44:14

标签: angularjs

我在Angular JS上有样本:

angular.module('wm-admin', []).
    config(function($routeProvider) {
        $routeProvider.
            when('/users', {controller:UsersController, templateUrl:'/public/html/crm/users/list.html'}).
            otherwise({redirectTo:'/users'});
    });

// Controllers //
function UsersController($scope) {

}

它给了我错误:

Uncaught Error: [$injector:modulerr]

那么,我做错了什么?

HTML:

<body ng-app="wm-admin">
</body>

我也试过任何代码:

Angular JS:

(function (angular) {
    'use strict';

    angular.module('wm-admin', [])
        .config(function($routeProvider) {
        $routeProvider.
            when('/users', {controller:UsersController, templateUrl:'/public/html/crm/users/list.html'}).
            otherwise({redirectTo:'/users'});
    })

    // Controllers //
    .controller('UsersController', ['$scope', '$http', function ($scope, $http) {

    }])

})(window.angular);

请查看代码上部

2 个答案:

答案 0 :(得分:2)

您尚未将控制器正确注入您的应用。添加以下行:

angular.module('wm-admin').controller('UsersController', UsersController);

修改

在您更新的问题中,您有以下代码:

(function (angular) {
    'use strict';

    angular.module('wm-admin', [])
        .config(function($routeProvider) {
        $routeProvider.
            when('/users', {controller:UsersController, templateUrl:'/public/html/crm/users/list.html'}).
            otherwise({redirectTo:'/users'});
    })

    // Controllers //
    .controller('UsersController', ['$scope', '$http', function ($scope, $http) {

    }])

})(window.angular);

但是现在UsersController不再是{controller:UsersController}行范围内的函数。将其更改为控制器:'UsersController'(字符串,不是对函数的引用):

(function (angular) {
    'use strict';

    angular.module('wm-admin', [])
        .config(function($routeProvider) {
        $routeProvider.
            when('/users', {controller: 'UsersController', templateUrl:'/public/html/crm/users/list.html'}).
            otherwise({redirectTo:'/users'});
    })

    // Controllers //
    .controller('UsersController', ['$scope', '$http', function ($scope, $http) {

    }])

})(window.angular);

答案 1 :(得分:1)

你从未真正成为控制者。

angular
    .module('wm-admin')
    .controller('UsersController', UsersController);

UsersController.$inject = ['$scope'];

function UsersController($scope) {
}