Skip to content

Commit 138c765

Browse files
Copilotrenemadsen
andcommitted
Use setTimeout instead of ChangeDetectorRef for better async handling
Co-authored-by: renemadsen <[email protected]>
1 parent 006041e commit 138c765

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

eform-client/src/app/modules/advanced/modules/navigation-menu/components/navigation-menu-page/navigation-menu-page.component.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, OnDestroy, OnInit, ViewChild, inject, AfterViewInit, ViewChildren, QueryList, ChangeDetectorRef } from '@angular/core';
1+
import { Component, OnDestroy, OnInit, ViewChild, inject, AfterViewInit, ViewChildren, QueryList } from '@angular/core';
22
import { CdkDragDrop, moveItemInArray, copyArrayItem, transferArrayItem, CdkDragMove, CdkDragRelease, CdkDropList, CdkDrag } from '@angular/cdk/drag-drop';
33
import {
44
NavigationMenuItemIndexedModel,
@@ -45,7 +45,6 @@ export class NavigationMenuPageComponent implements OnInit, OnDestroy, AfterView
4545
private overlay = inject(Overlay);
4646
private store = inject(Store);
4747
private dragDropService = inject(NavigationMenuDragDropService);
48-
private cdr = inject(ChangeDetectorRef);
4948

5049

5150
@ViewChild('resetMenuModal')
@@ -108,13 +107,15 @@ export class NavigationMenuPageComponent implements OnInit, OnDestroy, AfterView
108107

109108
// Subscribe to changes in drop lists (for dynamically added dropdowns)
110109
this.dropLists.changes.subscribe(() => {
111-
// Clear and re-register all drop lists when the list changes
112-
this.dragDropService.dropLists = [];
113-
this.dropLists?.forEach(dropList => {
114-
this.dragDropService.register(dropList);
110+
// Use setTimeout to defer the update to avoid ExpressionChangedAfterItHasBeenCheckedError
111+
// This ensures the update happens after the current change detection cycle
112+
setTimeout(() => {
113+
// Clear and re-register all drop lists when the list changes
114+
this.dragDropService.dropLists = [];
115+
this.dropLists?.forEach(dropList => {
116+
this.dragDropService.register(dropList);
117+
});
115118
});
116-
// Trigger change detection to prevent ExpressionChangedAfterItHasBeenCheckedError
117-
this.cdr.detectChanges();
118119
});
119120
}
120121
}

0 commit comments

Comments
 (0)