【问题标题】:AngularJS animate directiveAngularJS 动画指令
【发布时间】:2016-04-30 10:46:18
【问题描述】:

我想在我的指令中使用 $animate 添加一个类:

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;
}

但从未添加该类。

有什么想法吗?

更新 - 添加 plnkr:https://plnkr.co/edit/zczgsnLuXONfU8U5mIuv - 每次点击仅记录“[添加]”

【问题讨论】:

  • 发布您的完整代码或创建一个 plunkr
  • 您的指令工作正常。控制台显示什么?
  • plnkr 在更新后

标签: angularjs


【解决方案1】:
      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' );
          });
        }
      });

您可以查看this answer了解更多详情。

【讨论】:

    猜你喜欢
    • 2014-01-25
    • 2014-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-04
    • 1970-01-01
    相关资源
    最近更新 更多