0Pricing
Angular Academy · Lesson

Animation Basics: trigger and state

Define animation states and triggers.

Animation Basics: trigger and state is a free Angular Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Angular Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

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

Yes — the full text of “Animation Basics: trigger and state” is free to read here on the web, and the Angular Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Angular Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

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