23

I am trying to create a function that will be triggered at the end of an animation in Angular 2 (I am using the latest angular cli).

I have been on the Angular Animations to gain some understanding of how this would be implemented by assigning the trigger with a callback in my example of code I have a component that is animated onto the page. the code is has follows:

//first.component.ts

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


@Component({
  selector: 'app-first',
  templateUrl: './first.component.html',
  styleUrls: ['./first.component.css'],
  host: {
    '[@routeAnimation]': 'true',
    '[style.display]': "'block'",
    '[style.position]': "'absolute'"
  },
  animations: [
    trigger('routeAnimation', [
      state('*', style({transform: 'translateX(0)', opacity: 1})),
      transition('void => *', [style({transform: 'translateX(-100%)', opacity: 0}),animate(500)]),
      transition('* => void', animate(500, style({transform: 'translateX(100%)', opacity: 0})))
    ])
  ]
})
export class FirstComponent implements OnInit {

  constructor() { }

  ngOnInit() {

  }

  myFunc() {
  // call this function at the end of the animation.
 }

}

the html is simply a div

<div class="w9914420">
  <h2>
     first-component Works!
  </h2>
</div> 

To be honest I am not too familiar with JavaScript so any help or a quick example would help me gain a better understanding of Angular 2.

2 Answers 2

50

This is working example:

import {Component, NgModule, Input, trigger, state, animate, transition, style, HostListener } from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'

@Component({
  selector : 'toggle',
  animations: [
    trigger('toggle', [
      state('true', style({ opacity: 1; color: 'red' })),
      state('void', style({ opacity: 0; color: 'blue' })),
      transition(':enter', animate('500ms ease-in-out')),
      transition(':leave', animate('500ms ease-in-out'))
    ])
  ],
  template: `
  <div class="toggle" [@toggle]="show" 
        (@toggle.start)="animationStarted($event)"
        (@toggle.done)="animationDone($event)"
     *ngIf="show">
    <ng-content></ng-content>
  </div>`
})
export class Toggle {
  @Input() show:boolean = true;
  @HostListener('document:click')
  onClick(){
    this.show=!this.show;
  }

  animationStarted($event) {
    console.log('Start');
  }

  animationDone($event) {
    console.log('End');
  }
}

@Component({
  selector: 'my-app',
  template: `
    <div>
      <toggle>Hey!</toggle>
    </div>
  `,
})
export class App {

}

@NgModule({
  imports: [ BrowserModule ],
  declarations: [ App, Toggle ],
  bootstrap: [ App ]
})
export class AppModule {}

Plunker

Sign up to request clarification or add additional context in comments.

3 Comments

Good example of this using this method with NgModule, thank you PatrickJane.
I actually created a PLUNKER (plnkr.co/edit/d1UQcN?p=preview) and implemented the callback functions and added the start and done triggers to the user/component.ts file. I noticed a strange behaviour to the User component transition. This implementation causes a slight delay when the component is transition on the stage to remove the dashboard component. Any ideas to why this might be happening? removing the callback implementation sets back things to normal?
Have a look at my plunker tell me if you find the same issue as stated in my above comment
20

Now Angular2's supported (@animation.start) and (@animation.done) to tap into animation events.

For example, you can use (@routeAnimation.start)=onStart($event), (@routeAnimation.done)=onDone($event) in the first.component.html.

You can find out more at here.

Also you can see a simple example with the first.component.ts on Plunker.

Hope this help!

4 Comments

So how would you define the routeAnimation onstart and done functions in first.component.ts for example?
I appreciate the example, and demonstrates how I would implement with my code
actually created a PLUNKER (plnkr.co/edit/d1UQcN?p=preview) and implemented the callback functions and added the start and done triggers to the user/component.ts file. I noticed a strange behaviour to the User component transition. This implementation causes a slight delay when the component is transition on the stage to remove the dashboard component. Any ideas to why this might be happening? removing the callback implementation sets back things to normal?
Have a look at my plunker tell me if you find the same issue as stated in my above comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.