【问题标题】:AngularJS $animate.enter not adding ng-enter and ng-enter-active classesAngularJS $animate.enter 不添加 ng-enter 和 ng-enter-active 类
【发布时间】:2014-03-18 05:27:04
【问题描述】:

我正在尝试将$animate 服务合并到我自己的指令中。我无法进入和离开以实际制作动画。

奇怪的是,使用$animate.enter,元素被附加到DOM,并且回调函数被触发。但似乎从未添加 ng-animate、ng-enter 和 ng-enter-active 类。元素只是简单地附加到 DOM 中,没有动画。回调函数会触发,但它会立即触发,而不是在应该发生的动画持续时间之后触发。 leave 也会发生同样的事情;元素立即从 DOM 中删除,回调立即触发;没有动画。

<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8" />
    <title>$animate.enter</title>
    <script data-require="angular.js@1.2.x" src="http://code.angularjs.org/1.2.14/angular.js" data-semver="1.2.14"></script>
    <script data-require="angular.js@1.2.x" src="http://code.angularjs.org/1.2.5/angular-animate.js" data-semver="1.2.5"></script>

    <script type="text/javascript" charset="utf-8">
        var app = angular.module('TestAnimation', []);

        app.controller('TestAnimation', function($scope) {
        });

        app.directive("appendaroo", function($animate, $compile) {
          function link(scope, element, attr) {
            var isAppended = false;
            var parent = element.parent();
            var box;
            element.on('click', function() {
              isAppended = !isAppended;
              if (isAppended) {
                box = angular.element('<div class="rect"></div>');
                $animate.enter(box, parent, element, function() {
                  console.log("Done entering");
                });
              } else {
                $animate.leave(box, function() {
                  console.log("Done leaving");
                });
              }
            });
          }
          return {
            link: link
          };
        });
    </script>

    <style type="text/css" media="screen">
        .rect {
            width: 100px;
            height: 100px;
            background-color: #ff9933;
            transition: all 1s ease-out;
        }
        .rect.ng-enter,
        .rect.ng-leave.ng-leave-active {
            opacity: 0;
        }
        .rect.ng-enter.ng-enter-active,
        .rect.ng-leave {
            opacity: 1;
        }
    </style>

  </head>

  <body ng-controller="TestAnimation" ng-app="TestAnimation">
      <button appendaroo>Fade in/out</button>
  </body>

</html>

我对 Angular 很陌生,我想我只是错过了一些东西,所以如果这是一个疯狂愚蠢的问题,我深表歉意。但似乎没有很多资源可用于在您自己的指令中使用 $animate。

我可以毫无问题地使用$animate.addClass 和$animate.removeClass,这很有帮助,并且表明我走在正确的轨道上,但是enter 和leave 给我带来了问题。

我把例子放在Punker上:

http://plnkr.co/edit/XtvZAZzgA8ORZBaRN68d?p=preview

【问题讨论】:

  • 附带说明,$animate API 文档并没有说得很清楚,但之后您需要一个父 或。所以在你的代码中,这应该可以工作:animate.enter(box, null, element, function() { ... }。 (如果您查看 ngRepeat 的源代码,您会看到这里已经完成)。

标签: javascript angularjs animation angularjs-directive ng-animate


【解决方案1】:

要使用 ngAnimate 模块,您需要将其作为依赖项添加到您的模块中:

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

您没有得到任何异常的原因是基本模块包含一个$animate 服务,其默认实现如documentation 中所述(有点令人困惑):

不执行任何操作的 $animate 的默认实现 动画,而只是同步执行 DOM 更新和调用 done() 回调。

为了启用动画,必须加载 ngAnimate 模块。

要查看功能实现,请查看 src/ngAnimate/animate.js

将ngAnimate 模块添加为依赖项后,您的代码仍不会像您希望的那样运行。然而,这是因为一些完全不同的东西,与$animate 服务没有真正的关系:

.on() 是 Angular 的 jqLit​​e 中包含的 jQuery 方法。附加事件处理程序中的代码位于 Angular 之外,因此您需要使用 $apply:

$apply() 用于从外部以角度执行表达式 角度框架。 (例如来自浏览器 DOM 事件, setTimeout、XHR 或第三方库)。因为我们正在呼唤 我们需要执行适当的范围生命周期的角度框架 异常处理,执行手表。

像这样包装你的$animate 调用,你的代码就可以正常工作了:

scope.$apply(function () {
  $animate.enter(box, parent, element, function() {
    console.log("Done entering");
  });
});

scope.$apply(function () {
  $animate.leave(box, function() {
    console.log("Done leaving");
  });
});

演示: http://plnkr.co/edit/iWHBNyKfwiSUUFKrMQff?p=preview

【讨论】:

  • 感谢您的回答。我包含了模块,现在添加了一个框,但从未删除。由于现在编写代码(相同的链接,现在添加了ngAnimate),出现了一个框,但永远不会被删除。回调函数永远不会触发。当然,动画不会运行,因为ng-enter 等。人。永远不会添加类。我期望发生的是:您单击按钮,一个框淡入。您再次单击它,该框淡出。重复。
  • 非常感谢!这个 Angular 拼图有很多部分......感谢您帮助它在我的脑海中更好地融合在一起。 (我会支持你,但不幸的是我的名声不佳)
  • 我花了一个多小时试图弄清楚为什么我的动画东西不起作用。添加ngAnimate 作为依赖解决了这个问题。
  • 从 Angular 1.4.0 开始,您不需要将 $animate.enter 或 $animate.leave 包装成 scope.$apply
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-23
  • 1970-01-01
  • 2015-05-08
  • 2016-03-09
  • 2016-08-09
  • 2015-12-17
相关资源
最近更新 更多