Modules and Controllers
Organizing AngularJS code with angular.module and connecting controllers to the DOM.
What you'll learn
- Declare an AngularJS module with angular.module
- Register a controller on a module and connect it to the DOM with ng-controller
- Read an existing AngularJS file and identify its module/controller structure
Explanation
An AngularJS application is organized into modules, declared with angular.module("appName", [dependencies]) -- the array lists other modules this one depends on (empty [] for a module with no dependencies). This is how AngularJS composed larger applications from smaller, independently-testable pieces.
A controller is a JavaScript function registered on a module, responsible for setting up a section of $scope: app.controller("MainController", function($scope) { $scope.greeting = "Hello!"; }). It's connected to a specific part of the DOM with the ng-controller directive: <div ng-controller="MainController">{{greeting}}</div> -- everything inside that div has access to MainController's $scope.
When reading an existing AngularJS file, a reliable first step is identifying its module declaration (usually near the top of a file, or in a dedicated app.js) and then finding each .controller(...) (and later, .service(...)/.factory(...)) registration -- this quickly maps out the application's overall shape before you dig into any one piece's logic.
Guided lab
Fill in the blank: declaring vs. retrieving a module
Fill in the missing array argument that DECLARES a new module (vs. retrieving an existing one), then predict the output.
// This line must DECLARE a new module named "app" (not retrieve an existing one):
const app = angular.module("app", ____);
app.controller("MainController", function () {
return { greeting: "Hello from MainController" };
});
const controllerFactory = app._controllers = app._controllers || {};
console.log(app.name);Stuck? Get a hint.
Common mistakes
- Confusing angular.module("name", [...]) (declaring a NEW module, note the array argument) with angular.module("name") (retrieving an EXISTING module, no array) -- using the wrong form in the wrong place is a common real bug when editing legacy AngularJS code.
- Forgetting ng-controller scopes a controller to a specific DOM subtree -- code outside that subtree doesn't have access to its $scope.
- Not checking a file's module/controller registrations first when getting oriented in an unfamiliar AngularJS codebase, and instead diving straight into unfamiliar logic.
Knowledge check
Takeaway
angular.module declares/retrieves modules (array argument vs. none is the key distinction), and ng-controller scopes a controller's $scope to a DOM subtree.
Summary
Modules organize AngularJS apps; controllers set up $scope and connect to the DOM via ng-controller; mapping module/controller registrations is a reliable way to get oriented in legacy code.
References
Your notes
Notes save automatically.
Finished this lesson?
Mark it complete to track your progress and schedule a future review.
AI tutor
The optional AI tutor isn't enabled in this deployment. All lessons, exercises, quizzes, and search work fully without it.