【问题标题】:Disable ng-animate globally and then enable on some elements全局禁用 ng-animate,然后在某些元素上启用
【发布时间】:2017-01-24 16:59:32
【问题描述】:

使用 Angular 1.5.3,是否可以全局禁用 ng-animate,然后在某些元素上选择性地启用它?

我有一个指令 (ng-animate-enabled) 用于在容器 div 及其子容器上启用 ng-animate。

如果我通过 $animate.enabled(false) 在控制器中全局禁用 ng-animate,即使我通过上述指令专门启用了动画,动画也不再起作用。

这个jsfiddle 说明了问题。

谢谢!

【问题讨论】:

    标签: angularjs ng-animate


    【解决方案1】:

    最简单的方法是使用$animateProvider.classNameFilter()。这使您可以根据 HTML 类(或者更具体地说,针对该类检查的正则表达式)有选择地为某些元素启用 ngAnimate。这消除了启用 ngAnimate 的特定指令的需要,并且可以为您的应用程序提供显着的性能提升。在你的情况下,我会这样做:

    // Note that that the function takes a regular expression, not a string!
    // Don't put quotes around it, use forward slashes!
    $animateProvider.classNameFilter(/ng-animate-enabled/);
    

    这将为任何具有 ng-animate-enabled 类的元素启用 ngAnimate。

    有关更多信息,我建议您阅读有关该主题的Ben Nadel's blog post

    【讨论】:

    • 谢谢乔!我刚刚阅读了 Ben 的文章,他确认了我的问题,所以我删除了它(在你回答之前)。此外,this response 的第一条评论似乎澄清了我上面的原始方法是不可能的。
    • @rmirro:很高兴它帮助了你!
    猜你喜欢
    • 2011-02-13
    • 2019-09-28
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多