I'm trying to incorporate the $animate service into my own directive. I can't get enter and leave to actually animate.
The weird thing is that using $animate.enter, the element is appended to the DOM, and the callback function fires. But it seems as though the ng-animate, ng-enter, and ng-enter-active classes never get added. The element is simply appended to the DOM without animation. The callback function fires, but it fires instantly and not after the duration of the animation that's supposed to happen. The same thing happens with leave; the element is removed from the DOM instantly, and the callback fires instantly; no animation.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>$animate.enter</title>
<script data-require="angular.js@1.2.x" src="http://code.angularjs.org/1.2.14/angular.js" data-semver="1.2.14"></script>
<script data-require="angular.js@1.2.x" src="http://code.angularjs.org/1.2.5/angular-animate.js" data-semver="1.2.5"></script>
<script type="text/javascript" charset="utf-8">
var app = angular.module('TestAnimation', []);
app.controller('TestAnimation', function($scope) {
});
app.directive("appendaroo", function($animate, $compile) {
function link(scope, element, attr) {
var isAppended = false;
var parent = element.parent();
var box;
element.on('click', function() {
isAppended = !isAppended;
if (isAppended) {
box = angular.element('<div class="rect"></div>');
$animate.enter(box, parent, element, function() {
console.log("Done entering");
});
} else {
$animate.leave(box, function() {
console.log("Done leaving");
});
}
});
}
return {
link: link
};
});
</script>
<style type="text/css" media="screen">
.rect {
width: 100px;
height: 100px;
background-color: #ff9933;
transition: all 1s ease-out;
}
.rect.ng-enter,
.rect.ng-leave.ng-leave-active {
opacity: 0;
}
.rect.ng-enter.ng-enter-active,
.rect.ng-leave {
opacity: 1;
}
</style>
</head>
<body ng-controller="TestAnimation" ng-app="TestAnimation">
<button appendaroo>Fade in/out</button>
</body>
</html>
I'm rather new to Angular and I figure I'm just missing something, so apologies if this is a crazy stupid question. But there don't seem to be a lot of resources available for utilizing $animate in your own directives.
I am able to use $animate.addClass and $animate.removeClass without problem, which is helpful, and suggests that I'm on the right track, but enter and leave are giving me problems.
I put the example on Punker:
animate.enter(box, null, element, function() { ... }. (If you look at the source of ngRepeat, you'll see this done there). – Izhaki Jul 22 '14 at 21:26