diff --git a/aio/content/examples/animations/src/app/app.module.ts b/aio/content/examples/animations/src/app/app.module.ts index 0773357c58..b1ac87cca2 100644 --- a/aio/content/examples/animations/src/app/app.module.ts +++ b/aio/content/examples/animations/src/app/app.module.ts @@ -1,5 +1,6 @@ -// #docregion animations-module +// #docplaster import { NgModule } from '@angular/core'; +// #docregion animations-module import { BrowserModule } from '@angular/platform-browser'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; // #enddocregion animations-module @@ -15,11 +16,12 @@ import { HeroListAutoComponent } from './hero-list-auto.component'; import { HeroListGroupsComponent } from './hero-list-groups.component'; import { HeroListMultistepComponent } from './hero-list-multistep.component'; import { HeroListTimingsComponent } from './hero-list-timings.component'; +// #docregion animations-module -// #docregion animation-module @NgModule({ imports: [ BrowserModule, BrowserAnimationsModule ], - // #enddocregion animation-module + // ... more stuff ... +// #enddocregion animations-module declarations: [ HeroTeamBuilderComponent, HeroListBasicComponent, @@ -34,5 +36,8 @@ import { HeroListTimingsComponent } from './hero-list-timings.component'; HeroListGroupsComponent ], bootstrap: [ HeroTeamBuilderComponent ] +// #docregion animations-module }) export class AppModule { } +// #enddocregion animations-module + diff --git a/aio/content/examples/animations/src/app/hero-list-auto.component.ts b/aio/content/examples/animations/src/app/hero-list-auto.component.ts index 84b25c05de..6c9b70ff4d 100644 --- a/aio/content/examples/animations/src/app/hero-list-auto.component.ts +++ b/aio/content/examples/animations/src/app/hero-list-auto.component.ts @@ -10,7 +10,7 @@ import { transition } from '@angular/animations'; -import { Heroes } from './hero.service'; +import { Hero } from './hero.service'; @Component({ selector: 'hero-list-auto', @@ -43,5 +43,5 @@ import { Heroes } from './hero.service'; // #enddocregion animationdef }) export class HeroListAutoComponent { - @Input() heroes: Heroes; + @Input() heroes: Hero[]; } diff --git a/aio/content/examples/animations/src/app/hero-list-basic.component.ts b/aio/content/examples/animations/src/app/hero-list-basic.component.ts index 76d5ba686c..f3a5856d9d 100644 --- a/aio/content/examples/animations/src/app/hero-list-basic.component.ts +++ b/aio/content/examples/animations/src/app/hero-list-basic.component.ts @@ -14,7 +14,7 @@ import { } from '@angular/animations'; // #enddocregion imports -import { Heroes } from './hero.service'; +import { Hero } from './hero.service'; @Component({ selector: 'hero-list-basic', @@ -66,5 +66,5 @@ import { Heroes } from './hero.service'; // #enddocregion animationdef }) export class HeroListBasicComponent { - @Input() heroes: Heroes; + @Input() heroes: Hero[]; } diff --git a/aio/content/examples/animations/src/app/hero-list-combined-transitions.component.ts b/aio/content/examples/animations/src/app/hero-list-combined-transitions.component.ts index fc654cbcb5..cfac09b825 100644 --- a/aio/content/examples/animations/src/app/hero-list-combined-transitions.component.ts +++ b/aio/content/examples/animations/src/app/hero-list-combined-transitions.component.ts @@ -13,7 +13,7 @@ import { } from '@angular/animations'; // #enddocregion imports -import { Heroes } from './hero.service'; +import { Hero } from './hero.service'; @Component({ selector: 'hero-list-combined-transitions', @@ -55,5 +55,5 @@ import { Heroes } from './hero.service'; // #enddocregion animationdef }) export class HeroListCombinedTransitionsComponent { - @Input() heroes: Heroes; + @Input() heroes: Hero[]; } diff --git a/aio/content/examples/animations/src/app/hero-list-enter-leave-states.component.ts b/aio/content/examples/animations/src/app/hero-list-enter-leave-states.component.ts index c01e182e8b..1b5b1000f4 100644 --- a/aio/content/examples/animations/src/app/hero-list-enter-leave-states.component.ts +++ b/aio/content/examples/animations/src/app/hero-list-enter-leave-states.component.ts @@ -10,7 +10,7 @@ import { transition } from '@angular/animations'; -import { Heroes } from './hero.service'; +import { Hero } from './hero.service'; @Component({ selector: 'hero-list-enter-leave-states', @@ -59,5 +59,5 @@ import { Heroes } from './hero.service'; // #enddocregion animationdef }) export class HeroListEnterLeaveStatesComponent { - @Input() heroes: Heroes; + @Input() heroes: Hero[]; } diff --git a/aio/content/examples/animations/src/app/hero-list-enter-leave.component.ts b/aio/content/examples/animations/src/app/hero-list-enter-leave.component.ts index f27b5f10e1..3eed156873 100644 --- a/aio/content/examples/animations/src/app/hero-list-enter-leave.component.ts +++ b/aio/content/examples/animations/src/app/hero-list-enter-leave.component.ts @@ -10,7 +10,7 @@ import { transition } from '@angular/animations'; -import { Heroes } from './hero.service'; +import { Hero } from './hero.service'; @Component({ selector: 'hero-list-enter-leave', @@ -47,5 +47,5 @@ import { Heroes } from './hero.service'; // #enddocregion animationdef }) export class HeroListEnterLeaveComponent { - @Input() heroes: Heroes; + @Input() heroes: Hero[]; } diff --git a/aio/content/examples/animations/src/app/hero-list-groups.component.ts b/aio/content/examples/animations/src/app/hero-list-groups.component.ts index 12a57292f7..34fd8c207f 100644 --- a/aio/content/examples/animations/src/app/hero-list-groups.component.ts +++ b/aio/content/examples/animations/src/app/hero-list-groups.component.ts @@ -11,7 +11,7 @@ import { group } from '@angular/animations'; -import { Heroes } from './hero.service'; +import { Hero } from './hero.service'; @Component({ selector: 'hero-list-groups', @@ -76,5 +76,5 @@ import { Heroes } from './hero.service'; // #enddocregion animationdef }) export class HeroListGroupsComponent { - @Input() heroes: Heroes; + @Input() heroes: Hero[]; } diff --git a/aio/content/examples/animations/src/app/hero-list-inline-styles.component.ts b/aio/content/examples/animations/src/app/hero-list-inline-styles.component.ts index ed1bdb7646..8dab15c386 100644 --- a/aio/content/examples/animations/src/app/hero-list-inline-styles.component.ts +++ b/aio/content/examples/animations/src/app/hero-list-inline-styles.component.ts @@ -12,7 +12,7 @@ import { } from '@angular/animations'; // #enddocregion imports -import { Heroes } from './hero.service'; +import { Hero } from './hero.service'; @Component({ selector: 'hero-list-inline-styles', @@ -56,5 +56,5 @@ import { Heroes } from './hero.service'; // #enddocregion animationdef }) export class HeroListInlineStylesComponent { - @Input() heroes: Heroes; + @Input() heroes: Hero[]; } diff --git a/aio/content/examples/animations/src/app/hero-list-multistep.component.ts b/aio/content/examples/animations/src/app/hero-list-multistep.component.ts index 4e57896de5..f3aad073ab 100644 --- a/aio/content/examples/animations/src/app/hero-list-multistep.component.ts +++ b/aio/content/examples/animations/src/app/hero-list-multistep.component.ts @@ -12,7 +12,7 @@ import { AnimationEvent } from '@angular/animations'; -import { Heroes } from './hero.service'; +import { Hero } from './hero.service'; @Component({ selector: 'hero-list-multistep', @@ -59,7 +59,7 @@ import { Heroes } from './hero.service'; // #enddocregion animationdef }) export class HeroListMultistepComponent { - @Input() heroes: Heroes; + @Input() heroes: Hero[]; animationStarted(event: AnimationEvent) { console.warn('Animation started: ', event); diff --git a/aio/content/examples/animations/src/app/hero-list-timings.component.ts b/aio/content/examples/animations/src/app/hero-list-timings.component.ts index a300012e23..305cf78487 100644 --- a/aio/content/examples/animations/src/app/hero-list-timings.component.ts +++ b/aio/content/examples/animations/src/app/hero-list-timings.component.ts @@ -10,7 +10,7 @@ import { transition } from '@angular/animations'; -import { Heroes } from './hero.service'; +import { Hero } from './hero.service'; @Component({ selector: 'hero-list-timings', @@ -54,5 +54,5 @@ import { Heroes } from './hero.service'; // #enddocregion animationdef }) export class HeroListTimingsComponent { - @Input() heroes: Heroes; + @Input() heroes: Hero[]; } diff --git a/aio/content/examples/animations/src/app/hero-list-twoway.component.ts b/aio/content/examples/animations/src/app/hero-list-twoway.component.ts index 46d0c64a68..87e0109c7d 100644 --- a/aio/content/examples/animations/src/app/hero-list-twoway.component.ts +++ b/aio/content/examples/animations/src/app/hero-list-twoway.component.ts @@ -13,7 +13,7 @@ import { } from '@angular/animations'; // #enddocregion imports -import { Heroes } from './hero.service'; +import { Hero } from './hero.service'; @Component({ selector: 'hero-list-twoway', @@ -54,5 +54,5 @@ import { Heroes } from './hero.service'; // #enddocregion animationdef }) export class HeroListTwowayComponent { - @Input() heroes: Heroes; + @Input() heroes: Hero[]; } diff --git a/aio/content/examples/animations/src/app/hero-team-builder.component.ts b/aio/content/examples/animations/src/app/hero-team-builder.component.ts index e5413be50e..a46c41dcf0 100644 --- a/aio/content/examples/animations/src/app/hero-team-builder.component.ts +++ b/aio/content/examples/animations/src/app/hero-team-builder.component.ts @@ -1,40 +1,41 @@ import { Component } from '@angular/core'; -import { Heroes } from './hero.service'; +import { Hero, HeroService } from './hero.service'; @Component({ selector: 'hero-team-builder', template: `
- - - + + +
+

Basic State

Switch between active/inactive on click.

- +

Styles inline in transitions

-

Animated effect on click, no persistend end styles.

- +

Animated effect on click, no persistent end styles.

+

Combined transition syntax

Switch between active/inactive on click. Define just one transition used in both directions.

- +

Two-way transition syntax

Switch between active/inactive on click. Define just one transition used in both directions using the <=> syntax.

- +

Enter & Leave

Enter and leave animations using the void state.

- +
@@ -44,27 +45,27 @@ import { Heroes } from './hero.service'; Enter and leave animations combined with active/inactive state animations. Different enter and leave transitions depending on state.

- +

Auto Style Calc

Leave animation from the current computed height using the auto-style value *.

- +

Different Timings

Enter and leave animations with different easings, ease-in for enter, ease-out for leave.

- +

Multiple Keyframes

Enter and leave animations with three keyframes in each, to give the transition some bounce.

- +

Parallel Groups

Enter and leave animations with multiple properties animated in parallel with different timings.

- +
`, @@ -87,8 +88,12 @@ import { Heroes } from './hero.service'; min-height: 6em; } `], - providers: [Heroes] + providers: [HeroService] }) export class HeroTeamBuilderComponent { - constructor(private heroes: Heroes) { } + heroes: Hero[]; + + constructor(private heroService: HeroService) { + this.heroes = heroService.heroes; + } } diff --git a/aio/content/examples/animations/src/app/hero.service.ts b/aio/content/examples/animations/src/app/hero.service.ts index 6bdeb5a512..c0053d0185 100644 --- a/aio/content/examples/animations/src/app/hero.service.ts +++ b/aio/content/examples/animations/src/app/hero.service.ts @@ -1,16 +1,16 @@ import { Injectable } from '@angular/core'; -class Hero { - constructor(public name: string, - public state = 'inactive') { - } +// #docregion hero +export class Hero { + constructor(public name: string, public state = 'inactive') { } toggleState() { - this.state = (this.state === 'active' ? 'inactive' : 'active'); + this.state = this.state === 'active' ? 'inactive' : 'active'; } } +// #enddocregion hero -let ALL_HEROES = [ +const ALL_HEROES = [ 'Windstorm', 'RubberMan', 'Bombasto', @@ -25,36 +25,30 @@ let ALL_HEROES = [ ].map(name => new Hero(name)); @Injectable() -export class Heroes implements Iterable { +export class HeroService { - currentHeroes: Hero[] = []; - - [Symbol.iterator]() { - return this.currentHeroes.values(); - } + heroes: Hero[] = []; canAdd() { - return this.currentHeroes.length < ALL_HEROES.length; + return this.heroes.length < ALL_HEROES.length; } canRemove() { - return this.currentHeroes.length > 0; + return this.heroes.length > 0; } - addActive() { - let hero = ALL_HEROES[this.currentHeroes.length]; - hero.state = 'active'; - this.currentHeroes.push(hero); + addActive(active = true) { + let hero = ALL_HEROES[this.heroes.length]; + hero.state = active ? 'active' : 'inactive'; + this.heroes.push(hero); } addInactive() { - let hero = ALL_HEROES[this.currentHeroes.length]; - hero.state = 'inactive'; - this.currentHeroes.push(hero); + this.addActive(false); } remove() { - this.currentHeroes.splice(this.currentHeroes.length - 1, 1); + this.heroes.length -= 1; } } diff --git a/aio/content/guide/animations.md b/aio/content/guide/animations.md index 89ecd5f479..96e79948e8 100644 --- a/aio/content/guide/animations.md +++ b/aio/content/guide/animations.md @@ -28,19 +28,37 @@ The examples in this page are available as a . +## Setup + +Before you can add animations to your application, you need +to import a few animation-specific modules and functions to the root application module. + + + +#### Example basics + +The animations examples in this guide animate a list of heroes. + +A `Hero` class has a `name` property, a `state` property that indicates if the hero is active or not, +and a `toggleState()` method to switch between the states. + + + +Across the top of the screen (`app.hero-team-builder.component.ts`) +are a series of buttons that add and remove heroes from the list (via the `HeroService`). +The buttons trigger changes to the list that all of the example components see at the same time. + {@a example-transitioning-between-states} -## Quickstart example: Transitioning between two states +## Transitioning between two states A simple transition animation You can build a simple animation that transitions an element between two states driven by a model attribute. -Animations are defined inside `@Component` metadata. Before you can add animations, you need -to import a few animation-specific imports and functions: - +Animations can be defined inside `@Component` metadata.