[Angularjs]ng-repeat 完成的动画

发布时间: 2017/4/30 3:19:18
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我知道我可以使用 $animate 服务调用一个方法,当我手动执行动画到任何元 (像什么建议在这个答案),但我想检测时 ng-repeat 动画完成。

index.html

<div class="col-sm-6 col-md-4" ng-repeat="cat in cats">
  <div class="cat-container card">
    <div class="cat-header">
      <h4 ng-click="setActive($index)">{{cat.title}}</h4>
    </div>
    <div class="cat-body">
      <div ng-if="cat.img" class="cat-img">
        <img ng-src="{{cat.img}}">
      </div>
      <div class="sub-cats-container">
        <div class="sub-cat" ng-animate ng-repeat="subcat in cat.subcats | filter: getSearch($index)">
          <div class="cat-img">
            <img ng-src="{{subcat.img}}" alt="{{subcat.title}}" width="150px" height="70px">
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

动画

.sub-cat.ng-move,
.sub-cat.ng-enter,
.sub-cat.ng-leave {
    transition: all cubic-bezier(0.250, 0.460, 0.450, 0.940) .5s;
}

.sub-cat.ng-enter.ng-enter-active,
.sub-cat.ng-move.ng-move-active,
.sub-cat.ng-leave {
    opacity: 1;
    max-height: 70px;
}

.sub-cat.ng-leave.ng-leave-active,
.sub-cat.ng-enter {
    opacity: 0;
    max-height: 0px;
}

解决方法 1:

它是可能得到为回调 ng-enterng-move 使用 $animate:close 事件

例如 (使用 angularJS 1.3.4)︰

myApp.directive('subCat', function() {
    return {
        restrict: 'C',
        link: function(scope, element, attrs) {
          element.on('$animate:close', function() {
            // This will fire after every CSS or JavaScript animation is complete
            console.log('$animate:close');
          });
        }
    };
});

这里是说明这小提琴︰ http://jsfiddle.net/4wt4nr6s/

虽然工作不错,为 ng-enterng-move ,这似乎并没有为工作 ng-leave ,这是一个 bug,angularJS 还没有解决︰ https://github.com/angular/angular.js/issues/6049

赞助商