This is a heavily simplified demo of ng-class
animation using transition that I'm trying to achieve.
I'm trying to create a simple fade in animation when an item is selected.
Selecting an item will add a selected
class on it using ng-class
directive. I'm trying to add a transition to it following the documentation with the animation hook classes added by ng-animate
.
Below is the code I have so far. It sets the opacity
to 0
along with the transition property, and when the .selected-add-active
class is added it is supposed to transition to opacity
1. But it does not work.
angular.module('demo', ['ngAnimate'])
.controller('demoCtrl', function($scope) {
$scope.selected = false;
$scope.selectToggle = function() {
$scope.selected = !$scope.selected;
};
});
.item {
width: 50px;
height: 50px;
background: grey;
}
.item.selected {
background-color: dodgerblue;
}
.item.selected.selected-add {
transition: opacity 3s;
opacity: 0;
}
.item.selected.selected-add.selected-add-active {
opacity: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular-animate.js"></script>
<div ng-app="demo" ng-controller="demoCtrl">
<div class="item" ng-class="{selected:selected}"></div>
<br>
<br>
<button ng-click="selectToggle();">
{{selected? 'Unselect' : 'Select'}}
</button>
</div>
Why isn't my code working?