Search code examples
javascriptangularjscontrollers

Angular.js Novice to Ninja controllers


I have been trying to learn some Angular.js using Angular.js Novice to Ninja. The main problem that i have by now is that in the examples you have to files, app.js and controller.js.

The main module is declared in app.js and controllers.js as dependencies:

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

In controllers.js, controllers are declared like this:

angular.module('myApp.controllers').controller('BookController', function($scope){
$scope.name="Scope for BookController";
});

but it never works like that, i have to do this and keep everthing in the same file.

var app=angular.module('myApp', [
'myApp.controllers',
'ngRoute'
]);
app.controller('BookController', function($scope){
$scope.name="Scope for BookController";
});

Can someone tell me what is wrong? Thank you.


Solution

  • What does your debugger say?

    My guess is you're loading the files in the wrong order. You see, you have to first define myApp.controllers to be able to load it in myApp, so make sure your controllers.js is injected first, and then app.js.