This is what I use in Angular 8.1.2. The beauty of the code is that it supports the unlimited height of the div element to show / collapse, and also makes smooth transitions.
FILE TS:
import {Component, OnInit} from '@angular/core'; import {trigger, transition, animate, style, state} from '@angular/animations'; @Component({ selector: 'app-all-data', templateUrl: './all-data.page.html', styleUrls: ['./all-data.page.scss'], animations: [ trigger('openClose', [ state('open', style({ height: '*', opacity: 1, })), state('closed', style({ height: '0', opacity: 0 })), transition('open => closed', [ animate('0.35s') ]), transition('closed => open', [ animate('0.35s') ]), ]), ] }) export class AllDataPage implements OnInit { showCardBody = false; constructor() { } ngOnInit() { } showDetails() { this.showCardBody = !this.showCardBody; } }
HTML file:
<button type="button" (click)="showDetails()"> Toggle Details </button> <div class="card-body" [@openClose]="showCardBody ? 'open' : 'closed'"> <p>This is some content</p> <p>This is some content</p> <p>This is some content</p> <p>This is some content</p> <p>This is some content</p> </div>
Devner
source share