- 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 Animations
AngularJS provides support for animations through the ngAnimate module. This module doesn't perform the animations itself; instead, it provides "hooks" for CSS and JavaScript to tap into. These hooks allow you to add dynamic effects like fading, sliding, and scaling when elements are added to the DOM, removed, or changed. Animations are crucial for modern web development because they provide visual cues to the user, making the application feel more "alive" and responsive.
Key Concepts of AngularJS Animations
- ngAnimate Module: This is a separate library (usually
angular-animate.js) that you must include in your project and inject as a dependency. - CSS and JavaScript Animations: You can choose between CSS3 Transitions/Animations (the preferred method for performance) or JavaScript-defined animations (useful for complex logic or older browser support).
- Animation Hooks: These are specific CSS classes that AngularJS automatically applies to elements during their lifecycle, such as
.ng-enter,.ng-leave, and.ng-move.
How to Use AngularJS Animations
Step 1: Including the ngAnimate Module
Unlike the core AngularJS features, animations require an external file. You must include the script tag in your HTML and then declare the dependency in your main application module.
var app = angular.module('myApp', ['ngAnimate']);
Make sure to include the library in your HTML header or footer:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular-animate.js"></script>
angular-animate.js file before your app script, or forgetting to add 'ngAnimate' to your module's dependency array. Without both, your animation classes will simply be ignored.
Step 2: Animating Element Transitions
AngularJS tracks changes in the DOM. When an element is shown or hidden via directives, the framework attaches specific classes at the start and end of that transition.
Example: Fade In/Out Animation
<div ng-app="myApp" ng-controller="myCtrl">
<button ng-click="toggle()">Toggle Visibility</button>
<div ng-show="isVisible" class="fade-element">
This content will transition smoothly!
</div>
</div>
app.controller('myCtrl', function($scope) {
$scope.isVisible = true;
$scope.toggle = function() {
$scope.isVisible = !$scope.isVisible;
};
});
CSS for Animation:
To make the animation work, we define how the element looks when it is "entering" the view and when it is "leaving."
/* The base class defines the transition duration */
.fade-element {
transition: all opacity 0.5s ease-in-out;
}
/* Starting state for entering, ending state for leaving */
.fade-element.ng-hide-add,
.fade-element.ng-hide-remove {
transition: all 0.5s ease-in-out;
display: block !important;
}
.fade-element.ng-hide {
opacity: 0;
}
ng-show or ng-hide, AngularJS uses the .ng-hide class. Ensure your CSS doesn't accidentally override the display: none !important that AngularJS applies unless you are specifically animating it.
Step 3: Using ngAnimate with ng-show and ng-hide
The ng-show and ng-hide directives don't remove elements from the DOM; they toggle the .ng-hide class. This is great for elements that need to appear and disappear frequently without the overhead of being completely re-rendered.
Example: Slide Animation
<div ng-app="myApp" ng-controller="myCtrl">
<button ng-click="toggle()">Toggle Slide</button>
<div ng-show="isVisible" class="slide-box">Sliding content area</div>
</div>
.slide-box {
transition: 0.8s linear all;
opacity: 1;
}
.slide-box.ng-hide {
opacity: 0;
transform: translateX(-50px);
}
Step 4: Using ng-if with Animations
While ng-show toggles visibility, ng-if physically adds or removes the element from the DOM. This triggers the .ng-enter and .ng-leave hooks.
Example: Fade-in with ng-if
<div ng-if="isVisible" class="fade-if">
I am created and destroyed in the DOM.
</div>
.fade-if.ng-enter {
transition: 0.5s opacity;
opacity: 0;
}
.fade-if.ng-enter-active {
opacity: 1;
}
.fade-if.ng-leave {
transition: 0.5s opacity;
opacity: 1;
}
.fade-if.ng-leave-active {
opacity: 0;
}
ng-if is better for performance if the content inside is heavy (like a large map or a complex chart), because it isn't processed by the browser until it is needed.
Step 5: Animation with ngRepeat
One of the most powerful uses of ngAnimate is within lists. When you filter a list or add an item, ng-repeat can animate each individual item.
Example: Animate Items in List
<div ng-app="myApp" ng-controller="myCtrl">
<input type="text" ng-model="search" placeholder="Filter list...">
<ul>
<li ng-repeat="item in items | filter:search" class="list-animation">
{{ item }}
</li>
</ul>
</div>
.list-animation.ng-enter {
transition: 0.3s ease-out all;
opacity: 0;
transform: scale(0.9);
}
.list-animation.ng-enter-active {
opacity: 1;
transform: scale(1);
}
.list-animation.ng-leave {
transition: 0.2s ease-in all;
opacity: 1;
}
.list-animation.ng-leave-active {
opacity: 0;
transform: scale(0.5);
}
ng-repeat, keep the animations short (200ms–300ms). Long animations in lists can make the UI feel sluggish when a user is trying to filter quickly.
Step 6: Using JavaScript to Control Animations
Sometimes CSS isn't enough—for example, if you need to calculate an element's position dynamically or use an external animation library like GSAP. In these cases, you can use the $animate service.
Example: Triggering Animation Programmatically
app.controller('myCtrl', function($scope, $animate, $element) {
$scope.triggerAlert = function() {
// Manually add a class and let ngAnimate handle the lifecycle
$animate.addClass($element.find('p'), 'highlight-pulse').then(function() {
console.log('Animation finished!');
});
};
});
This approach allows you to run specific code after an animation finishes using the promise returned by $animate methods.
Animation States and Callbacks
To master AngularJS animations, you should familiarize yourself with these four primary states:
ng-enter: Triggered when an element is added to the DOM (ng-if,ng-repeat,ng-view,ng-include).ng-leave: Triggered when an element is removed from the DOM.ng-move: Triggered when an item changes position within anng-repeatlist.ng-hide-add/ng-hide-remove: Specific tong-showandng-hidedirectives.
Summary
AngularJS animations transform a static, "jumpy" interface into a polished user experience. By injecting the ngAnimate module, you gain access to powerful lifecycle hooks that work automatically with core directives like ng-if, ng-show, and ng-repeat. Whether you use CSS transitions for simple effects or the $animate service for complex JavaScript-driven logic, understanding the transition states (enter, leave, move) is key to building modern, interactive web applications.