简体   繁体   English

ngIf和儿童动画-Angular4

[英]ngIf and Children Animation - Angular4

I have the following template in Angular 4: 我在Angular 4中有以下模板:

<button (click)="isVisible = !isVisible" >Toggle</button>

<div class="global-block" *ngIf="isVisible" >
    <div class="content" [@slideRightLeft]>
       ... some content here ...
    </div>
    <div class="overlay" [@fadeIn]></div>
</div>

I already created both slideRightLeft and fadeIn animation using trigger and transition. 我已经使用触发器和过渡同时创建了slideRightLeftfadeIn动画。

The problem is that it works for the first toggle (when isVisible == true ), but not when I hide it (when isVisible == false , the .global-block is hidden instantly and children's animations aren't visible). 问题在于它适用于第一个切换(当isVisible == true ),但是当我隐藏它时则.global-block (当isVisible == false.global-block会立即隐藏并且儿童动画不可见)。

I really want to have 2 different animations for those children element, but I also need to toggle the .global-block 我确实想为这些children元素设置2种不同的动画,但是我还需要切换.global-block

Is there a way to delay the ngIf ? 有没有办法延迟ngIf or maybe my approach is not good? 还是我的方法不好?

Yea ngIf will nuke those from the DOM immediately, removing all descendants and their associated animations. 是的,如果将立即从DOM中删除它们,则删除所有后代及其关联的动画。 You could make it so the button only changes the toggle after a certain time period, waiting for the animations to complete, or you can use another method of hiding (visibility + opacity) which won't remove them from the DOM. 您可以这样做,以便按钮仅在特定时间段后更改切换,等待动画完成,或者可以使用另一种隐藏方法(可见性+不透明度)将其从DOM中删除。

You could probalby wrap it all into the animation states actually - may not even need to use ngIf. 您实际上可以把所有内容包装成动画状态-甚至可能不需要使用ngIf。 You can use a string to diffrentiate between any number of different animation states and style them accordingly. 您可以使用字符串来区分任意数量的不同动画状态,并相应地设置其样式。 So you could have one state that was 'button just clicked', another one 'button clicked 1000ms ago', and another 'animations over' (don't use those strings literally... but you get what i mean). 因此,您可以有一个状态为“刚刚单击按钮”,另一个状态为“ 1000毫秒前单击按钮”,以及另一个“动画结束”(不要从字面上使用这些字符串...但是您明白我的意思了)。 This would make the behavior much more predictable. 这将使行为更加可预测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM