according to the angular docs https://docs.angularjs.org/api/ng/directive/ngRepeat#animations
".enter - when a new item is added to the list or when an item is revealed after a filter
.leave - when an item is removed from the list or when an item is filtered out"
yet, when I .push({}) or .splice(-1, 1) from an array, neither of those classes are added to the ng-repeat. what's the problem?
<div ng-controller="MyCtrl">
<button ng-click="addLine()">
add
</button>
<button ng-click="removeLine()">
remove
</button>
<div ng-repeat="line in lines">
<div class="preview">{{$index}}</div>
</div>
</div>
var myApp = angular.module('myApp', ['ngAnimate']);
//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});
function MyCtrl($scope) {
$scope.addLine = function(){
$scope.lines.push({})
}
$scope.removeLine = function(){
$scope.lines.splice(-1, 1)
}
$scope.lines = [{
text: 'res1'},
{
text: 'res2'}];
}
as Ted noted in his answer, having actual css styles for .ng-enter / .ng-leave is required, otherwise the ngAnimate module will not add the .ng-enter classes to the DOM elements
To be more clear, I don't care about actually doing an animation right now. The issue for me here is that the .ng-enter classes don't actually get applied to the class attribute of an element unless you have css styles for .ng-enter
WORKING PLUNKER http://plnkr.co/edit/TqpdIy6syikBhAeb5Kw3?p=preview
The ngAnimate module provides support for CSS-based animations (keyframes and transitions) as well as JavaScript-based animations via callback hooks.
Wildcard statelinkAn asterisk * or wildcard matches any animation state. This is useful for defining transitions that apply regardless of the HTML element's start or end state. For example, a transition of open => * applies when the element's state changes from open to anything else.
The ngAnimate module adds and removes classes. The ngAnimate module does not animate your HTML elements, but when ngAnimate notice certain events, like hide or show of an HTML element, the element gets some pre-defined classes which can be used to make animations.
The State Change Expression instructs Angular when to run the transition's animations, it can either be. a string with a specific syntax. or a function that compares the previous and current state (value of the expression bound to the element's trigger) and returns true if the transition should occur or false otherwise.
For these classes to be added you have to load the animation module in your app.
See the ngAnimate docs for how to do this.
First you must load the additional module's .js file:
<script src="angular.js">
<script src="angular-animate.js">
Then list it as a dependent module of your application:
angular.module('app', ['ngAnimate']);
The ngAnimate module also requires the element to have its transition defined in either the CSS or the JS:
For CSS transitions, the transition code must be defined within the starting CSS class (in this case .ng-enter). The destination class is what the transition will animate towards.
If you add something like:
/* The starting CSS styles for the enter animation */
.fade.ng-enter {
transition:0.5s linear all;
opacity:0;
}
/* The finishing CSS styles for the enter animation */
.fade.ng-enter.ng-enter-active {
opacity:1;
}
It should start working.
To be very clear, because the docs don't say this explicitly: If the animation is not defined in either the CSS or the JS, the ngAnimate module won't even add the classes, it will just skip the animation all together.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With