This is a 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
source share