1

I am getting the 'Controller is not a function error in my application.

From what I have read the most common issue is that it the controller file is not referenced in the html page - this is not the case for me.

I do not get the error when I have included the controller in the same file as declaring the module, but when I am using seperate files for each controller.

Here is my folder structure:

app.js:

var myApp = angular.module('myApp', ['ngRoute']);

myApp.config(function ($routeProvider) {
    $routeProvider.when('/', {
        templateUrl: 'assets/views/mainView.html',
        controller: 'assets/controllers/mainController.js'
    });
});

mainController.js

var myApp = angular.module('myApp');

myApp.controller('mainController', ['$scope', function($scope){
    $scope.name = "test";
}]);

I have also read that when you declare a module, the array braces [] create a new module so I took them out of the module in mainController.js.

It works if I do this:

var myApp = angular.module('myApp', ['ngRoute']);

myApp.config(function ($routeProvider) {
    $routeProvider.when('/', {
        templateUrl: 'assets/views/mainView.html',
        controller: 'mainController.js'
    });
});


myApp.controller('mainController', ['$scope', function($scope){
    $scope.name = "hi";
}]);

I have referenced the controller like this in my index.html in the head section:

<script src="assets/scripts/js/app.js"></script>
<script src="assets/controllers/mainController.js"></script>
4
  • Remove var myApp = angular.module('myApp'); from controller file. Commented Mar 11, 2016 at 8:44
  • still the same result Commented Mar 11, 2016 at 8:50
  • you have given your controller's file path in config function like this : controller: 'assets/controllers/mainController.js' whereas as far i know it is the controller name and not path something like this in your case : controller: 'mainController'. Try doing this and let me know Commented Mar 11, 2016 at 9:10
  • thanks I posted the answer. Commented Mar 11, 2016 at 9:15

2 Answers 2

2

When you are having controller in another file no need to give path of the controller there which you have given in your config

    myApp.config(function ($routeProvider) {
$routeProvider.when('/', {
    templateUrl: 'assets/views/mainView.html',
     //Remove this
    //controller: 'assets/controllers/mainController.js'
     //and then try this
       controller: 'mainController'
   });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I would have never guessed that you dont need to specify the path to the controller...
1

Are you using IIFE? If not you should not assign angular module var myApp = angular.module('myApp'); on your mainController.js as it overwrite your myApp variable since it is treated as globe variable.

If you want to apply IIFE your code should be:

app.js

    (function () {
         var myApp = angular.module('myApp', ['ngRoute']);

        myApp.config(function ($routeProvider) {
            $routeProvider.when('/', {
                templateUrl: 'assets/views/mainView.html',
                controller: 'mainController'
            });
        });
    })();

mainController.js

    (function () {
        var myApp = angular.module('myApp');

        myApp.controller('mainController', ['$scope', function($scope){
            $scope.name = "test";
        }]);

    })();

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.