问题
in my project, I am using 2 different trees from angular material.
I created a demo on stackblitz here : https://stackblitz.com/edit/angular-wvbg5j.
When I try to access their data, I use treeControl.dataNodes but it only gives me the data from the latest generated tree ("groceries2 and reminders2 tree). It displays dataNodes in the console each time I check/uncheck an item.
So I don't know what to do, is there another way to access tree data? or is there a function that stores both trees in memory?
回答1:
Just add the data source as an argument when you handle the checkbox change :
(change)="todoLeafItemSelectionToggle(node, dataSourceX)"
And inside your function just access to the data attribute :
todoLeafItemSelectionToggle(node: TodoItemFlatNode, dataSource : MatTreeFlatDataSource<TodoItemNode, TodoItemFlatNode>): void {
console.log(dataSource.data) ;
this.checklistSelection.toggle(node);
this.checkAllParentsSelection(node);
this.getParentNode(node)
}
来源:https://stackoverflow.com/questions/58725906/how-to-access-data-from-2-different-mat-tree-in-angular