Skip to content

Commit b4e5620

Browse files
Licen-itAkshat55
andauthored
fix: deeply clone treeNodes to avoid Angular not detecting changes (#2966)
Co-authored-by: Akshat Patel <[email protected]>
1 parent 4c9f9e9 commit b4e5620

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/treeview/treeview.component.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class TreeViewComponent implements AfterViewInit, OnInit, OnDestroy {
7070
* Passing value will disregard projected content
7171
*/
7272
@Input() set tree(treeNodes: Node[]) {
73-
this._tree = treeNodes.map((node) => Object.assign({}, node));
73+
this._tree = treeNodes.map((node) => this.copyNode(node));
7474
this.treeViewService.contentProjected = false;
7575
}
7676

@@ -170,4 +170,13 @@ export class TreeViewComponent implements AfterViewInit, OnInit, OnDestroy {
170170
public isProjected() {
171171
return this.treeViewService.contentProjected;
172172
}
173+
174+
private copyNode(node: Node): Node {
175+
// making a recursive shallow copy to avoid performance issues when deeply cloning templateRefs if defined in the node
176+
const copiedNode = Object.assign({}, node);
177+
if (node.children) {
178+
copiedNode.children = node.children.map(child => this.copyNode(child));
179+
}
180+
return copiedNode;
181+
}
173182
}

0 commit comments

Comments
 (0)