0

I want to add a class using $animate in my directive:

app.directive( 'animTest', [ '$animate', function( $animate ) {
  return function( scope, element, attrs ) {
    element.on( 'click', function() {
      if( element.hasClass( 'clicked' ) ) {
        console.log( '[remove]' );
        $animate.removeClass( element, 'clicked' );
      } else {
        console.log( '[add]' );
        $animate.addClass( element, 'clicked' );
      }
    });
  };
}]);

CSS:

.clicked {
  background: red;
}
.clicked-add, .clicked-remove, .clicked-add-active, .clicked-remove-active {
  -webkit-transition: all linear 0.5s;
  transition: all linear 0.5s;
}

But the class is never added.

Any idea?

UPDATE - added plnkr: https://plnkr.co/edit/zczgsnLuXONfU8U5mIuv - logs only '[add]' to each click

Mat
  • 2,156
  • 2
  • 16
  • 29

1 Answers1

0
      element.on( 'click', function() {
        if( element.hasClass( 'clicked' ) ) {
          scope.$apply(function(){
            console.log( '[remove]' );
            $animate.removeClass( element, 'clicked' );
          })

        } else {
          scope.$apply(function(){
            console.log( '[add]' );
            $animate.addClass( element, 'clicked' );
          });
        }
      });

you can check this answer for more details.

Community
  • 1
  • 1
Ahmed Wagdi
  • 934
  • 5
  • 9