Search code examples
javascriptangularjsangularjs-ng-repeatcss-animationsng-animate

Can't seem to get ngAnimate to work on an ng-repeat


I have the following snippet of HTML:

<div ng-app ng-controller="mainCtrl">
  <div ng-repeat="item in items" ng-mouseenter="item.showRemove = true" ng-mouseleave="item.showRemove = false" class="repeated-item">
    <span class="remove-item glyphicon glyphicon-remove" ng-show="item.showRemove" ng-click="removeItem(item)"></span>
    <div class="h3">{{item.title}}</div>
    <p>{{item.description}}</p>
  </div>
</div>

The following CSS is applied to the markup:

body { padding: 15px; }

.repeated-item {
  padding: 10px;
  margin-bottom: 15px;
  border-left: solid 1px #ccc;
  border-top: solid 1px #ccc;
  border-radius: 10px;
  background-color: #eee;
  position: relative;
}

.repeated-item:hover { background-color: #ddd; }

.remove-item {
  float: right;
  cursor: pointer;
  color: #b00;
  font-size: x-large;
}

.h3 {
  border-bottom: dotted 1px #bbb;
}

.repeated-item.ng-leave {
  -webkit-transition:0.5s linear all;
  -moz-transition:0.5s linear all;
  -o-transition:0.5s linear all;
  transition:0.5s linear all;
  opacity:1;
}

.repeated-item.ng-leave.ng-leave-active {
  opacity:0;
}

Lastly, I have the following javascript:

function mainCtrl($scope) {
    $scope.items = [
      {
        title: 'Star Trek: The Original Series',
        description: 'brevity brevity...'
      },
      {
        title: 'Star Trek: The Next Generation',
        description: "brevity brevity..."
      },
      {
        title: 'Star Trek: Deep Space Nine',
        description: "brevity brevity..."
      },
      {
        title: 'Star Trek: Voyager',
        description: "brevity brevity..."
      },
      {
        title: 'Star Trek: Enterprise',
        description: "brevity brevity..."
      }
    ];
    $scope.removeItem = function (itemToRemove) {
      var index = $scope.items.indexOf(itemToRemove);
      if (index !== -1) {
        $scope.items.splice(index, 1);
      }
    };
}

I'm creating this little demo simply to learn how to use ngAnimate. However, the documentation on the web seems a little lacking and I can't seem to figure out what I'm doing wrong. I'm using this page to apply a fade-out animation to the items in the ng-repeat. Nothing I do seems to work. I've given the repeated item it's own class and I've applied the animation rules (yes I did include the ngAnimate file). Everything works except the animation.

What am I doing wrong?

Working demo on Codepen.io


Solution

  • Change <div ng-app ng-controller="mainCtrl"> to <div ng-app="ngAnimate" ng-controller="mainCtrl">

    Note the ngAnimate.