Skip to content
This repository was archived by the owner on Feb 2, 2025. It is now read-only.

Commit bcfc838

Browse files
committed
fix(#1118): Fix memory leak
1 parent 52236f5 commit bcfc838

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/angular-datatables.directive.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ export class DataTableDirective implements OnDestroy, OnInit {
3333
*/
3434
dtInstance: Promise<DataTables.Api>;
3535

36+
// Only used for destroying the table when destroying this directive
37+
private dt: DataTables.Api;
38+
3639
constructor(private el: ElementRef) { }
3740

3841
ngOnInit(): void {
@@ -49,15 +52,18 @@ export class DataTableDirective implements OnDestroy, OnInit {
4952
if (this.dtTrigger) {
5053
this.dtTrigger.unsubscribe();
5154
}
55+
if (this.dt) {
56+
this.dt.destroy(true);
57+
}
5258
}
5359

5460
private displayTable(): void {
5561
this.dtInstance = new Promise((resolve, reject) => {
5662
Promise.resolve(this.dtOptions).then(dtOptions => {
5763
// Using setTimeout as a "hack" to be "part" of NgZone
5864
setTimeout(() => {
59-
const dt = $(this.el.nativeElement).DataTable(dtOptions);
60-
resolve(dt);
65+
this.dt = $(this.el.nativeElement).DataTable(dtOptions);
66+
resolve(this.dt);
6167
});
6268
});
6369
});

0 commit comments

Comments
 (0)