0

I wanna display specific tree nodes <mat-tree-node> based on a node's nodeAuthorized: boolean property. How can I achieve this since angular does not allow more than 2 structural directives on a single element.

I've tried using the nodeAuthorized property on the only child <div> of <mat-tree-node>, but this renders an empty space between 2 or more tree nodes. Any help would be appreciated?

This is what I've tried but, this leaves an obvious empty space between nodes which I don't want.

<mat-tree-node *matTreeNodeDef="let node; when: hasChild" matTreeNodePadding>
 <div *ngIf="node.treeNode.nodeAuthorized">   
  <button mat-icon-button matTreeNodeToggle>
   <mat-icon class="mat-icon-rtl-mirror">
    {{
       treeControl.isExpanded(node) ? "expand_more" : "chevron_right"
     }}
    </mat-icon>
   </button>

   {{node.treeNode.nodeName}}
 </div>
</mat-tree-node>

1 Answer 1

1

*matTreeNodeDef can be used with <ng-container> which can then be used with another HTML element say <div> and structural directive *ngIf to render specific nodes.

<ng-container *matTreeNodeDef="let node">
    <ng-container *ngIf="node.authorized"> /** node rendering condition goes here */
      <mat-tree-node matTreeNodePadding>
        <button mat-icon-button matTreeNodeToggle>
          <mat-icon class="mat-icon-rtl-mirror">
            {{treeControl.isExpanded(node) ? 'expand_more' : 'chevron_right'}}
          </mat-icon>
        </button>
        {{node.name}}
      </mat-tree-node>
    </ng-container>
  </ng-container>
Sign up to request clarification or add additional context in comments.

2 Comments

Can you please share an example of this?
@Chandermani I have updated my answer with am example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.