Search code examples
angularjsangularjs-controller

Angular Controller is not defined?


Error: [ng:areq] http://errors.angularjs.org/1.3.15/ng/areq?p0=homeController&p1=not%20aNaNunction%2C%20got%20undefined

I am getting the above error

Controller.js

angular.module('app').controller('homeController', function($scope) {});

app.js

 var app = angular.module('app', ['base', 'ngRoute', 'routeResolverServices']);
angular.bootstrap(document, ['app']);
return app;

Solution

  • This doesn't work because of the order of your scripts (and wouldn't work in any order the way you defined it). When Controller.js loads first, there is is still no module named "app". When app.js loads first, it immediately bootstraps the app without the controller.

    It's best to define one module per file, for example:

    feature1.js

    angular.module("feature1", []).controller("homeController", function(){});
    

    app.js

    var app = angular.module('app', 
                 ['base', 'feature1', 'ngRoute', 'routeResolverServices']);
    app.bootstrap(document, ['app']);
    

    and load app.js last:

    <script src="feature1.js"></script>
    <scirpt src="app.js"></script>