0

I added an animation class to a div with a variable.

 <div class="{{ItemsChange}}"></div>

In my controller:

$scope.addItem=function(item){
  $rootScope.ItemsChange=''
  $rootScope.ItemsChange='animated pulse'
 }

Now, I want this class to be applied everytime addItem is called, This is exactly why I am first adding empty string to variable and then the animation class so that animation class 'animated pulse' is applied each time $scope.addItem is called but animation class gets applied only the first time.

How do I fix this?

6
  • I'm not entirely sure what your hoping to accomplish, but you might look at the ngAnimate module to help with animations inside of AngularJS. If you are asking what I think you are asking, that module should help you get the job done. Commented Sep 18, 2015 at 13:44
  • That's exactly what i am using. animated pulse is a class from animate.css only Commented Sep 18, 2015 at 13:47
  • Could you update your question with your actual styles? Also, what directive are you expecting to watch with ngAnimate? You might try using ngClass instead of just class. Commented Sep 18, 2015 at 13:51
  • I meant that I am using daneden.github.io/animate.css for animation. Animate.css is coming from there and so is animated pulse CSS class. Only matter is that I want this class to be applied on every function call. Commented Sep 18, 2015 at 13:54
  • OK, class isn't animation aware, per ngAnimate documentation. Try using ngClass instead. Commented Sep 18, 2015 at 13:58

1 Answer 1

1

Sorry, I had assumed animate.css was optimized for Angular animations, but it isn't. To do things the way you are, just reset $scope.ItemsChagne inside a $timeout. So, add $timeout as a dependency to your controller and use

$scope.addItem = function(item) {
    $scope.ItemsChange = 'animated pulse';
    /**
     * each animation inside animate.css is one second, 
     * so give it an extra few milliseconds 
     */
    $timeout(function() {
        $scope.ItemsChange = false;
    }, 1100); 
});

It's not particularly pretty, but it does work.

EDIT

Try using ngClass instead like so:

ng-class="{animated: ItemsChange, pulse: ItemsChange}"

On plnkr

Sign up to request clarification or add additional context in comments.

6 Comments

It still did not. I tried setting ItemsChange to almost everything. Null ,empty string , undefined but still It does not switch :(
It still did not. I tried setting ItemsChange to almost everything. Null ,empty string , undefined but still It does not switch :(
No. No errors. Just that it does not animate again once the class has been assigned.
See my update, using ngClass should make that work. What we want is for .animated.pulse to be removed from the classList once the animation has completed. ngClass should do that.
No, It still animated only the first time :(
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.