0
0
Angularframework~20 mins

Transition between states in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Angular Animation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when this Angular animation triggers?

Consider this Angular component with a simple fade animation between two states. What will the user see when the state changes from 'open' to 'closed'?

Angular
import { Component } from '@angular/core';
import { trigger, state, style, animate, transition } from '@angular/animations';

@Component({
  selector: 'app-fade-box',
  template: `<div [@fadeState]="state" (click)="toggle()" tabindex="0" aria-label="Toggle box visibility">Click me</div>`,
  animations: [
    trigger('fadeState', [
      state('open', style({ opacity: 1 })),
      state('closed', style({ opacity: 0 })),
      transition('open => closed', [animate('500ms ease-out')]),
      transition('closed => open', [animate('300ms ease-in')])
    ])
  ]
})
export class FadeBoxComponent {
  state = 'open';
  toggle() {
    this.state = this.state === 'open' ? 'closed' : 'open';
  }
}
AThe box smoothly fades out over 500ms when changing from 'open' to 'closed'.
BThe box instantly disappears without animation when changing from 'open' to 'closed'.
CThe box fades in over 300ms when changing from 'open' to 'closed'.
DThe box fades out over 300ms when changing from 'open' to 'closed'.
Attempts:
2 left
💡 Hint

Look at the transition definitions and their timing.

📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this Angular animation trigger

Which option correctly fixes the syntax error in this Angular animation trigger?

Angular
trigger('slideState', [
  state('start', style({ transform: 'translateX(0)' })),
  state('end', style({ transform: 'translateX(100%)' })),
  transition('start => end', animate('400ms ease-in')),
  transition('end => start' animate('400ms ease-out'))
])
AReplace '=>' with '->' in the transition definitions
BAdd a comma between the last transition and animate: transition('end => start', animate('400ms ease-out'))
CRemove the comma after the first transition line
DChange 'animate' to 'animation' in the last transition
Attempts:
2 left
💡 Hint

Check punctuation between function arguments.

state_output
advanced
2:00remaining
What is the final state value after these toggles?

Given this Angular component code, what is the value of state after calling toggle() three times starting from 'closed'?

Angular
export class SlideComponent {
  state = 'closed';
  toggle() {
    this.state = this.state === 'open' ? 'closed' : 'open';
  }
}
A'closed'
Bnull
Cundefined
D'open'
Attempts:
2 left
💡 Hint

Trace the toggle calls step by step.

🔧 Debug
advanced
2:00remaining
Why does this Angular animation not trigger on state change?

In this Angular component, the animation does not run when the state changes. What is the most likely cause?

Angular
import { Component } from '@angular/core';
import { trigger, state, style, animate, transition } from '@angular/animations';

@Component({
  selector: 'app-box',
  template: `<div [@boxState]="state" (click)="toggle()">Box</div>`,
  animations: [
    trigger('boxState', [
      state('visible', style({ opacity: 1 })),
      state('hidden', style({ opacity: 0 })),
      transition('visible => hidden', animate('400ms')),
      transition('hidden => visible', animate('400ms'))
    ])
  ]
})
export class BoxComponent {
  state = 'visible';
  toggle() {
    this.state = this.state === 'visible' ? 'hidden' : 'visible';
  }
}
AThe component is missing the BrowserAnimationsModule import in the app module.
BThe animation durations are too short to notice.
CThe toggle method does not update the state variable.
DThe animation trigger name does not match the template binding.
Attempts:
2 left
💡 Hint

Check module imports required for animations.

🧠 Conceptual
expert
2:00remaining
Which Angular animation transition will NOT run?

Given these animation transitions, which one will never trigger during normal state changes?

Angular
trigger('moveState', [
  state('start', style({ transform: 'translateX(0)' })),
  state('middle', style({ transform: 'translateX(50%)' })),
  state('end', style({ transform: 'translateX(100%)' })),
  transition('start => middle', animate('300ms')),
  transition('middle => end', animate('300ms')),
  transition('start => end', animate('600ms'))
])
ATransition from 'middle' to 'end'
BTransition from 'start' to 'middle'
CTransition from 'end' to 'start'
DTransition from 'start' to 'end'
Attempts:
2 left
💡 Hint

Look at the defined transitions and their directions.