尝试使按钮折叠/展开Angular Material Nested Tree时出错

时间:2019-04-16 16:09:09

标签: angular tree material

我有一棵角材料嵌套树。我正在尝试创建一个按钮来展开/折叠,但出现以下错误:
ERROR TypeError: Cannot read property 'reduce' of undefined

我已经使用HTML尝试过:

<button (click)="ahtree.treeControl.collapseAll()">collapseAll</button>

<button (click)="treeControl.expandAll()">expandAll</button>


<mat-tree #ahtree [dataSource]="AccountRelTypeTreeDataSource" [treeControl]="treeControl" class="example-tree">
  <!-- This is the tree node template for leaf nodes -->
  <mat-tree-node *matTreeNodeDef="let node" matTreeNodeToggle>
    <li class="mat-tree-node">
      <!-- use a disabled button to provide padding for tree leaf -->
      <button mat-icon-button disabled></button>
      <p">{{node.displayName}}</p>
    </li>
  </mat-tree-node>
  // The rest...
  ...

或使用打字稿:

  // Trees:
  @ViewChild('ahtree') ahtree;
  selectedTab = 0;

  // Datasource
  treeControl = new NestedTreeControl<TreeNode>(node => node.children);
  DataSource = new MatTreeNestedDataSource<TreeNode>();

  ...

   ngAfterViewInit() {
    this.ahtree.treeControl.expandAll();
   }

我读了https://material.angular.io/components/tree/overview#treecontrol,但似乎仍然无法正常工作。

有人知道如何使用嵌套树吗?

1 个答案:

答案 0 :(得分:0)

您没有显示完整的堆栈跟踪,但是我想错误与此Github ticket中描述的错误相同。用户devversion提供的解决方案对我有用。在同一票证中查看此anchor