- Angular JS Tutorial
- AngularJS Home
- AngularJS Introduction
- AngularJS Expressions
- AngularJS Directives
- AngularJS Modules
- AngularJS Controllers
- AngularJS Scopes
- AngularJS Data Binding
- AngularJS Services
- AngularJS HTTP
- AngularJS Filters
- AngularJS Forms
- AngularJS Validation
- AngularJS DOM Manipulation
- AngularJS Select
- AngularJS Tables
- AngularJS Events
- AngularJS Routing
- AngularJS Includes
- AngularJS Animations
- AngularJS Dependency Injection
- AngularJS API
- AngularJS Deployment
AngularJS Controllers
In the world of AngularJS, controllers are the "brains" behind your view. They act as a vital bridge between your data (the Model) and what the user sees on the screen (the View). While the View is responsible for the layout, the controller is responsible for the logic—deciding how data should be displayed, handling user clicks, and managing the state of a specific part of your application.
Technically, a controller is a JavaScript constructor function that is used to augment the AngularJS Scope. When a controller is attached to the DOM via the ng-controller directive, AngularJS instantiates a new Controller object, creating a isolated environment for your logic.
Key Responsibilities of Controllers
- Manage Scope: Controllers set up the initial state of the
$scopeobject, which acts as the "glue" between the JavaScript code and the HTML template. - Business Logic: They house the logic for specific UI features, such as calculating totals in a shopping cart or filtering a list of users.
- Data Initialization: Controllers are the perfect place to define default values so that your page doesn't look empty when it first loads.
- Interaction with Services: Instead of fetching data directly, controllers "ask" services (like
$http) for data and then prepare that data for the view. - Event Handling: They respond to user actions, such as form submissions, button clicks, or mouse hovers, using directives like
ng-clickorng-submit.
Creating a Controller
To create a controller, you use the .controller() method on your AngularJS module. This method takes two arguments: the name of the controller and a factory function (or an array for dependency injection) that defines its behavior.
Syntax:
var app = angular.module('myApp', []);
app.controller('ProfileController', function($scope) {
// Initial data
$scope.user = {
name: 'Jane Doe',
role: 'Full Stack Developer'
};
});
- app: The variable representing your AngularJS module.
- 'ProfileController': The unique name of your controller. Use PascalCase by convention.
- $scope: A built-in AngularJS object that passes data between the controller and the view. Anything attached to
$scopebecomes automatically available in your HTML.
$scope as an argument in the function. If you try to use $scope.variable without declaring $scope in the function parameters, your code will throw an "undefined" error.
Example: Real-World Profile Editor
Let's look at a practical example. Imagine a user profile card where a user can toggle between a "view" mode and an "edit" mode.
<!DOCTYPE html>
<html>
<head>
<title>User Profile Editor</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
</head>
<body ng-app="profileApp">
<div ng-controller="UserCtrl">
<div class="card">
<h2>User: {{ user.name }}</h2>
<p>Status: {{ user.status }}</p>
<!-- Input field bound to the same data -->
<input type="text" ng-model="user.name" ng-show="isEditing">
<button ng-click="toggleEdit()">
{{ isEditing ? 'Save' : 'Edit Name' }}
</button>
</div>
</div>
<script>
var app = angular.module('profileApp', []);
app.controller('UserCtrl', function($scope) {
// Initializing Data
$scope.user = {
name: "Alex Smith",
status: "Active"
};
$scope.isEditing = false;
// Logic to handle user interaction
$scope.toggleEdit = function() {
$scope.isEditing = !$scope.isEditing;
if (!$scope.isEditing) {
alert("Profile updated to: " + $scope.user.name);
}
};
});
</script>
</body>
</html>
Explanation:
- Initialization: When the page loads,
$scope.userand$scope.isEditingare set. The view immediately renders "Alex Smith" because of the{{ user.name }}binding. - Two-Way Data Binding: The
ng-model="user.name"directive means that as the user types in the input box, the$scope.user.nameproperty updates in real-time. - Conditional Visibility:
ng-show="isEditing"hides or shows the input box based on the boolean value in the controller. - Functionality: The
toggleEditfunction changes the state of the UI, showing how controllers manage "view state."
document.getElementById) inside a controller. In AngularJS, the controller should only modify data; let directives handle the DOM.
Advanced Controller Techniques
- Dependency Injection (Minification Safe): When you minify JavaScript code for production, variable names like
$scopeget shortened toaorb, which breaks AngularJS. Use the array syntax to stay safe:app.controller('SafeCtrl', ['$scope', '$http', function($scope, $http) { ... }]); - ControllerAs Syntax: Modern AngularJS development often avoids
$scopein favor of thecontroller assyntax. This treats the controller like a standard JavaScript class instance.<div ng-controller="MainCtrl as main"> {{ main.title }} </div> - Controller Lifecycle Hooks: Use
this.$onInit = function() { ... }for initialization logic. This is a cleaner approach than just putting code at the top of the function, especially when dealing with component-based architecture.
Best Practices for Controllers
- One Responsibility: A controller should manage exactly one view. If your controller is 500 lines long, it's time to break it into smaller controllers or move logic into services.
- Avoid Global Functions: Never define functions outside the controller or module scope. Keep the global namespace clean.
- Use Services for Persistence: Controllers are destroyed when the user navigates away from a view. If you need data to persist (like a user's login session), store it in a Service, not a Controller.
console.log($scope) during development to inspect what data is currently available to your view. It's the fastest way to debug binding issues.
Summary
Controllers in AngularJS are the essential connectors that bring your application to life. They handle the "why" and "how" of your UI logic, managing the data flow between your backend services and your frontend HTML. By keeping them focused, lightweight, and logic-oriented, you ensure your AngularJS applications remain maintainable and scalable as they grow.