Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

210
Vistas
Why ng2-tree is showing undefined

I'm using ng2-tree https://angular2-tree.readme.io/v3.2.0/docs/inputs plugin

When i input below json it is showing as undefined

[
    {
        "value": "helper",
        "name": "helper",
        "children": []
    },
    {
        "value": "taxi",
        "name": "taxi",
        "children": []
    },
    {
        "value": "Cake",
        "name": "Cake",
        "children": [
            {
                "name": "Chocolate Fudge Cake",
                "value": "Chocolate Fudge Cake"
            },
            {
                "name": "Carrot & Walnut Cake",
                "value": "Carrot & Walnut Cake"
            }
        ]
    }
]

with above json my result is as undefined you can see them in my provided link below

here is the stackblitz link: https://stackblitz.com/edit/angular-ng2-tree-aouyza?file=app/app.component.ts

Please help me thanks in advance!!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Your data structure is wrong. The tree component received as input param a TreeModel and you're having an array of TreeModels at the moment.

Either you adjust your data structure and use a parent TreeModel to wrap your current ones as its children, like following:

tree: TreeModel = {
  value: 'Parent Model',
  children: [
    {
      value: 'helper',
      name: 'helper',
      children: [],
    },
    {
      value: 'taxi',
      name: 'taxi',
      children: [],
    },
    {
      value: 'Cake',
      name: 'Cake',
      children: [
        {
          name: 'Chocolate Fudge Cake',
          value: 'Chocolate Fudge Cake',
        },
        {
          name: 'Carrot & Walnut Cake',
          value: 'Carrot & Walnut Cake',
        },
      ],
    }
  ]
};

Or you iterate over the array in the HTML and use multiple tree components. That would look like following:

<tree [tree]="t" *ngFor="let t of tree"></tree>

For more information see the Github page of ng2-tree ;)

Update:

You still need to adjust the data model the way I suggested but you can hide the empty root node. To do so, you need to do following:

HTML

<tree [tree]="tree" [settings]="{ rootIsVisible: false }"></tree>

Due to this setting a class rootless is applied which hides the empyt root node but only if you've added node_modules/ng2-tree/styles.css to your angular.json or you've added a custom implementation for that class.

You can find the settings doc here.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda