3

I am using angular 5 and the angular-tree-component https://github.com/500tech/angular-tree-component. I would like to style one node different from the others

import { Component, ViewChild} from '@angular/core';
import { TreeComponent } from 'angular-tree-component';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {

  @ViewChild('myTree')
  private tree: TreeComponent;

  nodes = [
    {
      id: 1,
      name: 'root1',
      children: [
        { id: 2, name: '<b>child1</b>' },
        { id: 3, name: 'child2' }
      ]
    }
  ];
}

So the Problem is, that the b tags are sanitized by angular to \lt b \gt \lt b \gt.

How can I pragmatically generate HTML in angular?

1
  • If you have stackblitz sample, Kindly share, we are also stuck with drawing dotted lines in tree view component's nodes. Commented Mar 16, 2020 at 10:46

2 Answers 2

10

You can define a custom treeNodeTemplate. This will allow you to add custom classes to each node template.

<tree-root [nodes]="nodes" [options]="options">
  <ng-template #treeNodeTemplate let-node let-index="index">
    <span [ngClass]="node.data.classes">{{ node.data.name }}</span>
  </ng-template>
</tree-root>

then pass your css classes to the node data

nodes = [
  {
    id: 1,
    name: 'root1',
    classes: ['text-bold'],
    children: [...]
  }
];

and finally define that class

.text-bold{
  font-weight: 700;
}

You could also update the template to just use the innerHTML directive so you can pass in html to the name property.

<ng-template #treeNodeTemplate let-node let-index="index">
  <span [innerHTML]="node.data.name"></span>
</ng-template>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Your answer was very helpful!
0

Be sure to apply ng deep

scss

/deep/ {
  .tree-wrapper {
    .tree-children {
      padding-left: 5px;
    }
  }
}

html

<div class="tree-wrapper">
    <tree-root
      [nodes]="menuItems | async | tableFilter: [menuFilterTerm]"
      [options]="options"
      (activate)="onSelectMenuItem($event)"
    >
    </tree-root>
  </div>

Comments

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.