0

I'm trying to add a directive that animates the element when it is clicked, but the code in my directive never seems to trigger.

Fiddle

html:

<img class="refresh" animate-spin src="refresh-icon-614x460.png">

js:

myApp.directive('animateSpin', ['$animate', function($animate) {
  return function(element, scope, attrs) {
    element.on('click', function() {
        $animate.addClass(element, 'spin');
    });
  };
}]);
4
  • Your example uses AngularJS v1.0.5, is that the version you are asking for? Commented May 2, 2014 at 15:27
  • @tasseKATT Updated to use newer version Commented May 2, 2014 at 15:42
  • Will answer later when I have time. Commented May 2, 2014 at 16:09
  • I think I figured out what I was doing wrong. (everything) I'll post my own solution in a bit Commented May 2, 2014 at 16:15

1 Answer 1

4

I realized that I was using $animate all wrong. Here's the solution I came up with. Clicking the directive will add the spin class to the element, as well as the spin-add class. the animation will run and the spin class will be removed so that it can be added again next click.

directiveModule.directive('animateSpin', ['$animate',
    function ($animate) {
        return {
            link: function (scope, elem, attrs) {
                elem.on('click', function () {
                    var self = angular.element(this);
                    $animate.addClass(self, 'spin', function () {
                        self.removeClass('spin');
                    });
                });
            }
        };
}]);

CSS:

.spin-add{
   -webkit-animation:spin 4s linear;
    -moz-animation:spin 4s linear;
    animation:spin 4s linear; 
}
Sign up to request clarification or add additional context in comments.

1 Comment

@IanB you cannot just change an answer by editing it and posting additional code without pointing this out to the author first because it might not have been his intent.

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.