Angular 2动画结束回调函数的示例

Angular 2动画结束回调函数的示例

问题描述:

我正在尝试创建一个函数,该函数将在Angular 2中的动画结束时触发(我正在使用最新的角度cli).

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.
 }

}

html只是一个div

the html is simply a div

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

说实话,我对JavaScript不太熟悉,因此任何帮助或简单的示例都可以帮助我更好地了解Angular 2.

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.

这是可行的示例:

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 {}

柱塞