Djavad
Djavad

Reputation: 31

Uncaught Error: [$injector:modulerr] in AngularJS

I have sample on Angular JS as:

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) {

}

It gives me error:

Uncaught Error: [$injector:modulerr]

So, what I do wrong?

HTML:

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

I tried also any code:

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);

Look please code upper

Upvotes: 1

Views: 75

Answers (2)

yvesmancera
yvesmancera

Reputation: 2925

You haven't injected the controller properly to your app. Add this line:

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

EDIT

In your updated question you have this code:

(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);

But now UsersController is no longer a function within the scope of the {controller: UsersController} line. Change it to controller: 'UsersController' (string, not reference to a function):

(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);

Upvotes: 2

kiswa
kiswa

Reputation: 14987

You never actually made a controller.

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

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

function UsersController($scope) {
}

Upvotes: 1

Related Questions