我已经稍微修改了你的代码。您可以在此处查看演示:https ://plnkr.co/edit/S7YdfUZN2t0fey9pgo6x?p=preview
//our root app component
import {Component, NgModule, VERSION} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { trigger, state, style, transition, animate } from '@angular/animations';
@Component({
selector: 'my-app',
template: `
<div>
<button (click) ="openReportsFilter()">Open Close</button>
<h2 *ngIf="openCloseAnim" [@expandCollapse] = 'openCloseAnim'>Hello {{name}}</h2>
</div>
`,
animations: [
trigger('expandCollapse', [
state('expandCollapseState', style({height: '*'})),
transition('* => void', [style({height: '*'}), animate(1000, style({height: "0"})) ]),
transition('void => *', [style({height: '0'}), animate(1000, style({height: "*"})) ])
]
})
export class App {
name:string;
openCloseAnim: boolean = true;
constructor() {
this.name = `Angular! v${VERSION.full}`
}
openReportsFilter(): void {
console.log('clicked');
this.openCloseAnim = !this.openCloseAnim;
console.log(this.openCloseAnim);
//this.openCloseAnim = (this.openCloseAnim == true) ? false : true;
}
}
@NgModule({
imports: [ BrowserModule,BrowserAnimationsModule ],
declarations: [ App ],
bootstrap: [ App ]
})
export class AppModule {
}