Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

207
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda