What is angular-animate?
The angular-animate package is an AngularJS module that provides support for JavaScript, CSS3 transition, and CSS3 keyframe animation hooks within your AngularJS applications. It allows you to create smooth, complex animations for various elements and states in your application.
What are angular-animate's main functionalities?
CSS-based animations
This feature allows you to define CSS classes for entering and leaving animations. The .ng-enter and .ng-leave classes are used to define the starting state of the animation, while the .ng-enter-active and .ng-leave-active classes define the end state.
/* CSS code */
.fade.ng-enter {
transition: 0.5s linear all;
opacity: 0;
}
.fade.ng-enter-active {
opacity: 1;
}
.fade.ng-leave {
transition: 0.5s linear all;
opacity: 1;
}
.fade.ng-leave-active {
opacity: 0;
}
JavaScript-based animations
This feature allows you to define animations using JavaScript. You can create custom animations for entering and leaving elements by manipulating their CSS properties and using jQuery's animate function.
// JavaScript code
angular.module('myApp', ['ngAnimate'])
.animation('.slide', function() {
return {
enter: function(element, done) {
element.css({
position: 'relative',
left: '-10px',
opacity: 0
});
element.animate({
left: '0px',
opacity: 1
}, 1000, done);
},
leave: function(element, done) {
element.css({
position: 'relative',
left: '0px',
opacity: 1
});
element.animate({
left: '-10px',
opacity: 0
}, 1000, done);
}
};
});
ngAnimate directive
The ngAnimate directive can be used to apply animations to elements within an AngularJS application. In this example, the .fade class is applied to each item in the ng-repeat directive, and the corresponding CSS animations are triggered when items are added or removed.
<div ng-app="myApp" ng-controller="myCtrl">
<div ng-repeat="item in items" class="fade">
{{item}}
</div>
</div>
<script>
angular.module('myApp', ['ngAnimate'])
.controller('myCtrl', function($scope) {
$scope.items = ['Item 1', 'Item 2', 'Item 3'];
});
</script>
Other packages similar to angular-animate
ng2-animate
ng2-animate is an Angular (2+) module that provides a set of reusable animations for Angular applications. It offers a variety of pre-built animations that can be easily applied to elements, making it simpler to add animations without writing custom CSS or JavaScript. Compared to angular-animate, ng2-animate is designed for newer versions of Angular and provides a more modern approach to animations.
react-transition-group
react-transition-group is a popular library for managing animations in React applications. It provides components like Transition, CSSTransition, and TransitionGroup to handle the entering and exiting of elements with animations. While it serves a similar purpose to angular-animate, it is specifically designed for React and offers a different API and set of features tailored to React's component-based architecture.
animejs
animejs is a lightweight JavaScript animation library that works with any JavaScript framework, including AngularJS. It provides a powerful and flexible API for creating complex animations with minimal code. Unlike angular-animate, which is tightly integrated with AngularJS, animejs is a general-purpose animation library that can be used across different frameworks and projects.
packaged angular-animate
This repo is for distribution on npm
and bower
. The source for this module is in the
main AngularJS repo.
Please file issues and pull requests against that repo.
Install
You can install this package either with npm
or with bower
.
npm
npm install angular-animate
Then add ngAnimate
as a dependency for your app:
angular.module('myApp', [require('angular-animate')]);
bower
bower install angular-animate
Then add a <script>
to your index.html
:
<script src="/bower_components/angular-animate/angular-animate.js"></script>
Then add ngAnimate
as a dependency for your app:
angular.module('myApp', ['ngAnimate']);
Documentation
Documentation is available on the
AngularJS docs site.
License
The MIT License
Copyright (c) 2010-2015 Google, Inc. http://angularjs.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
1.6.0-rc.1 proximity-warning (2016-11-21)
New Features
- ngModelOptions: allow options to be inherited from ancestor
ngModelOptions
(296cfc #10922) - $compile: set
preAssignBindingsEnabled
to false by default (bcd0d4 #15352)
Bug Fixes
- ngModelOptions: handle debounce of
updateOn
triggers that are not in debounce list (789790) - ngMock/$controller: respect
$compileProvider.preAssignBindingsEnabled()
(7d9a79) - $location: throw if the path starts with double (back)slashes (4aa953)
- core: do not auto-bootstrap when loaded from an extension. (0ff10e)
- input[radio]: use strict comparison when evaluating checked-ness (5ac7da #15288)
Reverts
- ngModelOptions: allow options to be inherited from ancestor ngModelOptions (fb0225)
Performance Improvements
- ngOptions: avoid calls to
element.value
(3b7f29)
Breaking Changes
- feat($compile): set preAssignBindingsEnabled to false by default (bcd0d4):
Previously, $compileProvider.preAssignBindingsEnabled
was
set to true by default. This means bindings were pre-assigned in component
constructors. In AngularJS 1.5+ the place to put the initialization logic
relying on bindings being present is the controller $onInit
method.
To migrate follow the example below:
Before:
angular.module('myApp', [])
.component('myComponent', {
bindings: {value: '<'},
controller: function() {
this.doubleValue = this.value * 2;
}
});
After:
angular.module('myApp', [])
.component('myComponent', {
bindings: {value: '<'},
controller: function() {
this.$onInit = function() {
this.doubleValue = this.value * 2;
};
}
});
If you don't have time to migrate the code at the moment, you can flip the
setting back to true:
angular.module('myApp', [])
.config(function($compileProvider) {
$compileProvider.preAssignBindingsEnabled(true);
})
.component('myComponent', {
bindings: {value: '<'},
controller: function() {
this.doubleValue = this.value * 2;
}
});
Don't do this if you're writing a library, though, as you shouldn't change
global configuration then.
- fix(input[radio]): use strict comparison when evaluating checked-ness (5ac7da):
When using input[radio], the checked status is now determined by doing
a strict comparison between the value of the input and the ngModel.$viewValue.
Previously, this was a non-strict comparison (==).
This means in the following examples the radio is no longer checked:
<!-- this.selected = 0 -->
<input type="radio" ng-model="$ctrl.selected" value="0" >
<!-- this.selected = 0; this.value = false; -->
<input type="radio" ng-model="$ctrl.selected" ng-value="$ctrl.value" >
The migration strategy is to convert values that matched with non-strict
conversion so that they will match with strict conversion.
- feat(ngModelOptions): allow options to be inherited from ancestor
ngModelOptions
(296cfc):
The programmatic API for ngModelOptions
has changed. You must now read options
via the ngModelController.$options.getOption(name)
method, rather than accessing the
option directly as a property of the ngModelContoller.$options
object. This does not
affect the usage in templates and only affects custom directives that might have been
reading options for their own purposes.
One benefit of these changes, though, is that the ngModelControler.$options
property
is now guaranteed to be defined so there is no need to check before accessing.
So, previously:
var myOption = ngModelController.$options && ngModelController.$options['my-option'];
and now:
var myOption = ngModelController.$options.getOption('my-option');
<a name="1.6.0-rc.0"></a>