Getting Angular ng-class to $apply before an ng-repeat animation

浪尽此生 提交于 2020-01-03 17:29:54

问题


I have an animation that animates an element in an ng-repeat to the left or right depending on which button is clicked.

In one manoeuvre, I set a ng-class (the animation class) and then remove the element, which triggers the animation, but it doesn't seem to recognise the change to the ng-class before the animation is applied, unless I use a $scope.$apply(), but this throws up a $apply already in progress error. Is there a way to not have to use the $scope.$apply(), or to get rid of that error?

Here is the working fiddle (with errors). http://jsfiddle.net/noducks/6pFr2/

HTML

<div ng-controller="MyCtrl" style="text-align: center">
<div ng-repeat="elem in elements" ng-class="elem.anim">
   <button ng-click="out(elem, 'left', $index)">Left</button>
   <button ng-click="out(elem, 'right', $index)">Right</button>
</div>
</div>

Javascript

var myApp = angular.module('myApp',['ngAnimate']);

function MyCtrl($scope) {
$scope.elements = [
    {anim: ''},
    {anim: ''},
    {anim: ''},
    {anim: ''},
    {anim: ''}
];

$scope.out = function(elem, direc, index) {
    elem.anim = direc;
    $scope.$apply();
    $scope.elements.splice(index, 1);
};
}

CSS

.left.ng-leave {
  -webkit-transition:all linear 1s;
  transition:all linear 1s;
}

.left.ng-leave.ng-leave-active{
    -ms-transform: translateX(-100%); 
    -o-transform: translateX(-100%);
    -moz-transform: translateX(-100%);
    -webkit-transform: translateX(-100%);
    transform: translateX(-100%);
}

.left.ng-leave {
  -ms-transform: translateX(0%); 
  -o-transform: translateX(0%);
  -moz-transform: translateX(0%);
  -webkit-transform: translateX(0%);
  transform: translateX(0%);
}

.right.ng-leave {
    -webkit-transition:all linear 1s;
    transition:all linear 1s;
}

.right.ng-leave.ng-leave-active {
    -ms-transform: translateX(100%); 
    -o-transform: translateX(100%);
    -moz-transform: translateX(100%);
    -webkit-transform: translateX(100%);
    transform: translateX(100%);
}

.right.ng-leave {
    -ms-transform: translateX(0%); 
    -o-transform: translateX(0%);
    -moz-transform: translateX(0%);
    -webkit-transform: translateX(0%);
    transform: translateX(0%);
}

回答1:


The problem is that your repeated elements do not have any css animation information if they are removed from the DOM. I guess you have noticed that the elements are immediately removed from the DOM if you remove the $apply call. Also you may have noticed that the animation happens as expected if you hardcode the animation e.g. set class="left" or class="right".

To make the ngAnimation happen as you expect the $animate service and the browser need the information what you are trying to animate. But these informations are known to the browser and the $animate service only when the DOM manipulations has taken place.

How to solve this: you need to make the changes to the $scope.elements after the css class is updated at the DOM. So you need to delay the DOM manipulation for one digest loop. This can be done by the $timeout service (please see this answer for more information AngularJS : $evalAsync vs $timeout):

$scope.out = function(elem, direc, index) {
    elem.anim = direc;
    $timeout(function(){
       $scope.elements.splice(index, 1);
    });
}; 


来源:https://stackoverflow.com/questions/21663676/getting-angular-ng-class-to-apply-before-an-ng-repeat-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!