【问题标题】:Directive newly created in new scope with $compile does not ng-animate使用 $compile 在新范围内新创建的指令不会 ng-animate
【发布时间】:2014-10-12 20:47:55
【问题描述】:

我有一个switcher 指令,它有一个创建新范围的方法,并在其上使用$compile,将其element 替换为另一个指令lister

例如,lister 指令包含一个在新作用域上迭代的 ng-repeat。

我尝试在使用ng-enterng-leave 类创建新创建的lister 指令后立即对其进行动画处理,但它没有任何效果,或者看起来如此。

是否可以对使用$scompile 动态创建的指令进行动画处理?如果是这样,我错过了什么?

代码在this plunkr

【问题讨论】:

    标签: angularjs angularjs-directive css-transitions ng-animate angularjs-compile


    【解决方案1】:

    您的选择器是问题所在:-

    应该是.list-directive li,因为 li 是 ng-repeated。

    .list-directive li.ng-enter,
    .list-directive li.ng-leave {
        -webkit-transition: all 1s ease;
        -moz-transition: all 1s ease;
        -o-transition: all 1s ease;
        transition: all 1s ease;
    }
    .list-directive li.ng-enter {
        margin-left: 200px;
        opacity: 0;
    }
    
    .list-directive li.ng-leave,
    .list-directive li.ng-enter-active {
        margin-left: 0;
        opacity: 1;
    }
    .list-directive li.ng-leave-active {
        margin-left: -200px;
        opacity: 0;
    }
    

    Plnkr

    或者在li's 上应用一个类并对其应用规则。 Plnkr

    【讨论】:

    • 这行得通,但为什么呢?如果我手动将 margin-left 样式应用于 .list-directive div,它实际上会向右移动。为什么我不能对动画做同样的事情?
    • 编辑了我的问题,抱歉 :)
    • @janesconference 很抱歉,我不明白您的问题是什么。 margin-left 在 css 动画中只对吗?
    • 好吧,让我们说:如果我想在实际的 ng-repeat 之前为 List 单词设置动画怎么办?或者如果lister 指令真的很复杂,我只想为容器 div 设置动画以动画其中包含的所有内容?
    • @janesconference 那是因为 ng animate 将类应用于 ng-repeat/ng-show/ng-hide.. 等的项目......当你只是向 DOM 添加一些东西时怎么能ng 重复动画它,除非您在容器上切换某些属性,如 ng-show 并对其应用规则?
    猜你喜欢
    • 2015-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-17
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    • 2015-03-14
    相关资源
    最近更新 更多