0
0
Angularframework~10 mins

Enter and leave animations in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Enter and leave animations
Component loads
Element added to DOM
Trigger enter animation
Animation runs
Animation ends
Element visible
Trigger leave animation
Animation runs
Animation ends
Element removed from DOM
When an element appears, Angular triggers an enter animation before showing it. When it disappears, Angular triggers a leave animation before removing it from the DOM.
Execution Sample
Angular
import { Component } from '@angular/core';
import { trigger, transition, style, animate } from '@angular/animations';

@Component({
  selector: 'app-fade',
  template: `<div *ngIf="isVisible" @fade>Fade me in and out</div>`,
  animations: [
    trigger('fade', [
      transition(':enter', [style({opacity: 0}), animate('500ms', style({opacity: 1}))]),
      transition(':leave', [animate('500ms', style({opacity: 0}))])
    ])
  ]
})
export class FadeComponent {
  isVisible = true;
}
Defines a fade animation that runs when an element enters or leaves the DOM.
Execution Table
StepTriggerAnimation StateActionElement Visibility
1Component loadsNo animationElement not in DOMHidden
2Element addedEnter animation startsOpacity from 0 to 1 over 500msFading in
3After 500msEnter animation endsElement fully visibleVisible
4Element removedLeave animation startsOpacity from 1 to 0 over 500msFading out
5After 500msLeave animation endsElement removed from DOMHidden
💡 Element is removed from DOM after leave animation finishes
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5
elementOpacityN/A0 -> animating to 111 -> animating to 0N/A (removed)
elementInDOMfalsetruetruetruefalse
Key Moments - 3 Insights
Why does the element stay visible after the enter animation ends?
Because after the enter animation finishes (step 3), the element's opacity is fully 1 and it remains in the DOM visible as shown in the execution_table.
When exactly is the element removed from the DOM?
The element is removed only after the leave animation completes (step 5), not immediately when the element is removed from the template (step 4).
What happens if the animation duration is changed?
The animation runs for the new duration, so the fading in or out takes longer or shorter, reflected in the timing between steps 2-3 or 4-5 in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the elementOpacity value after step 3?
AAnimating from 0 to 1
B0
C1
DAnimating from 1 to 0
💡 Hint
Check the 'elementOpacity' row in variable_tracker after step 3
At which step does the element get removed from the DOM?
AStep 5
BStep 2
CStep 4
DStep 3
💡 Hint
Look at the 'elementInDOM' variable in variable_tracker and the 'Action' column in execution_table
If the enter animation duration is increased, how does it affect the execution_table?
ALeave animation runs longer
BStep 3 happens later, enter animation runs longer
CStep 2 is skipped
DElement is removed earlier
💡 Hint
Refer to the timing between steps 2 and 3 in execution_table
Concept Snapshot
Angular enter and leave animations:
- Use trigger() with :enter and :leave transitions
- :enter runs when element is added to DOM
- :leave runs before element is removed
- Animations run before visibility changes
- Element removed only after leave animation ends
Full Transcript
This visual execution shows how Angular handles enter and leave animations. When a component loads and an element is added, Angular triggers the enter animation, gradually changing opacity from 0 to 1 over 500 milliseconds. The element becomes fully visible after the animation ends. When the element is removed, Angular triggers the leave animation, fading opacity from 1 to 0 over 500 milliseconds before removing the element from the DOM. Variables like elementOpacity and elementInDOM track the element's state through these steps. Key moments include understanding that the element stays visible after enter animation ends and is only removed after leave animation finishes. Changing animation duration affects how long these transitions take. The quizzes test understanding of opacity values, removal timing, and animation duration effects.