node.name is not working in Component.ts in Angular

215 Views Asked by At

I need to get the root node name of a tree in angular, but node.name is getting "Node" as the value.

Below is my .html code part:

<mat-tree [dataSource]="dataSource" [treeControl]="treeControl"
 class="example-tree">
    <mat-tree-node *matTreeNodeDef="let node" matTreeNodeToggle 
     (click)="selectedItem = node.name;" >
        {{node.name}}
    </mat-tree-node>
    <mat-nested-tree-node *matTreeNodeDef="let node; when: hasChild"
    >
        <div class="mat-tree-node">
          <button mat-icon-button matTreeNodeToggle
                  [attr.aria-label]="'Toggle ' + node.name"
                  (click)="selectedItem = node.name; NodeCollapse(node.name)">
            <mat-icon class="mat-icon-rtl-mirror">
              {{treeControl.isExpanded(node) ? 'expand_more' : 'chevron_right'}}
            </mat-icon>
          </button>
          {{node.name}}
        </div>
        <div [class.example-tree-invisible]="!treeControl.isExpanded(node)"
            role="group">
          <ng-container matTreeNodeOutlet></ng-container>
      </div>
    </mat-nested-tree-node>
  </mat-tree>

Below is my Component.ts code containing the Method(NodeCollapse) definition which I call on Node Button-Icon Click:

NodeCollapse(n_name:string):void{

let nodename:string=n_name;

if(Node.name)
{
  
}

}

Below is my source debugger from Browser: Browser Source debugger

1

There are 1 best solutions below

0
On

You are checking another Node. Also, you have already passed the node name to the NodeCollapse method.

let nodename:string=n_name;

// its wrong
if(Node.name)
{
  
}

If you want to access the name property of node entity, it's better to pass directly the node to the NodeCollapse method. Just use the n_name inside your method.

I have prepared an example.