似乎无法让ngAnimate在ng-repeat上工作

时间:2014-06-16 21:36:14

标签: javascript angularjs angularjs-ng-repeat css-animations ng-animate

我有以下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>

以下CSS适用于标记:

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;
}

最后,我有以下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);
      }
    };
}

我正在创建这个小小的演示,只是为了学习如何使用ngAnimate。然而,网络上的文档似乎有点缺乏,我似乎无法弄清楚我做错了什么。我正在使用this page将淡出动画应用于ng-repeat中的项目。我所做的一切似乎都没有用。我给了它自己的类重复的项目,我已经应用了动画规则(是的,我确实包含了ngAnimate文件)。一切都有效,除了动画。

我做错了什么?

Working demo on Codepen.io

1 个答案:

答案 0 :(得分:2)

<div ng-app ng-controller="mainCtrl">更改为<div ng-app="ngAnimate" ng-controller="mainCtrl">

请注意ngAnimate