Angular Academy · Lesson

Animation Basics: trigger and state

Define animation states and triggers.

Lesson 1 of 413 steps

Animation Basics: trigger and state is a free Angular Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Angular Academy learning path, and your progress syncs across the web and the CoddyKit app. The Angular Academy course includes 4 lessons in total.

Angular Animations

Angular's animation system is a declarative DSL built on the Web Animations API. You describe states and transitions, and Angular handles the timing and DOM updates.

Setup

Provide animations in your bootstrap config to enable the animation engine.

bootstrapApplication(AppComponent, {
  providers: [provideAnimationsAsync()]
});

The trigger

A trigger names an animation and groups its states and transitions. You attach it to an element via a binding.

animations: [
  trigger('openClose', [
    // states and transitions
  ])
]

Defining States

state binds a named state to a set of CSS styles the element should have while in that state.

state('open', style({ height: '200px', opacity: 1 })),
state('closed', style({ height: '0px', opacity: 0 }))

The style Function

style takes an object of CSS properties. Use camelCase keys or quoted CSS names.

style({ backgroundColor: '#1976d2', transform: 'scale(1)' })

Binding the Trigger

In the template, bind @triggerName to an expression that returns the current state.

<div [@openClose]="isOpen ? 'open' : 'closed'">
  Content
</div>

A Complete Trigger

Here is a trigger with two states and a transition between them.

trigger('openClose', [
  state('open', style({ height: '200px' })),
  state('closed', style({ height: '0px' })),
  transition('open <=> closed', animate('300ms ease'))
])

Driving State from the Component

The component just toggles a boolean or string; Angular interpolates between the matching state styles.

isOpen = signal(true);
toggle() { this.isOpen.update(v => !v); }

Wildcard State

The wildcard * matches any state, useful for defaults and enter/leave animations covered later.

state('*', style({ opacity: 1 }))

Void State

The special void state represents an element that is not in the DOM, the basis for enter and leave animations.

Why Declarative Animations?

Declarative animations keep timing logic out of components, integrate with Angular's lifecycle, and automatically clean up when elements leave the DOM.

Quick Check

Test your understanding of animation basics.

Recap

You learned that Angular animations use a trigger containing state definitions (each mapping a name to style), bound in the template via [@trigger]. The void and * states cover absent and any states.

Free to start

Learn HTML with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
38
Lessons
144

Frequently Asked Questions

Is the “Animation Basics: trigger and state” lesson free?

Yes — any 3 lessons of the Angular Academy learning path, including “Animation Basics: trigger and state”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Angular Academy course includes 4 lessons in total.

What will I learn in “Animation Basics: trigger and state”?

Define animation states and triggers. You practise Angular Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Angular Academy?

No prior experience is required. Angular Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “Animation Basics: trigger and state” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Angular Academy lesson?

Yes. Every Angular Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Animation Basics: trigger and state
  2. Transitions and Timing
  3. Enter and Leave Animations
  4. Keyframes and Staggering
← Back to Angular Academy