简体   繁体   English

如何在 angular 7 中使用 ngFor 将嵌套的 json 循环到 ul li

[英]How to loop nested json into ul li using ngFor in angular 7

I have a nested json,I need to append that json format into ul li tag using ngFor as per below output format,I can use ngfor loop but I am not getting how to use it.Here is the expected output(hardcoded) in html.Below is the code. I have a nested json,I need to append that json format into ul li tag using ngFor as per below output format,I can use ngfor loop but I am not getting how to use it.Here is the expected output(hardcoded) in html .下面是代码。

home.component.html home.component.html

<div class="col-md-3" id="leftNavBar">
  <ul>
    <li class="parentNav">parent1</li>
    <li class="childData">
      <ul>
        <li>child11<span class="pull-right"><input type="checkbox"></span></li>
        <li>child12<span class="pull-right"><input type="checkbox"></span></li>
      </ul>
    </li>
  </ul>
  <ul>
    <li class="parentNav">parent2</li>
    <li class="childData">
      <ul>
        <li>child2<span class="pull-right"><input type="checkbox"></span></li>
      </ul>
    </li>
  </ul>
  <ul>
    <li class="parentNav">parent3</li>
    <li class="childData">
      <ul>
        <li>child3<span class="pull-right"><input type="checkbox"></span></li>
      </ul>
    </li>
  </ul>

</div>

home.component.ts home.component.ts

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit { 

    nestedjson:any;
 constructor(private formBuilder: FormBuilder) {


     }

  ngOnInit() {
      this.nestedjson = [{"name":"parent1","value":["child11","child12"]},{"name":"parent2","value":["child2"]},{"name":"parent3","value":["child3"]}];

} 




}

That's very basic.这是非常基本的。 You should try it yourself first.你应该先自己试试。

在此处输入图像描述

<div class="col-md-3" id="leftNavBar">
    <ul *ngFor="let nested of nestedjson">
        <li class="parentNav">{{ nested.name }}</li>
        <li class="childData">
            <ul>
                <li *ngFor="let val of nested.value">{{ val }}<span class="pull-right"><input type="checkbox"></span></li>
            </ul>
        </li>
    </ul>
</div>

Stackblitz: https://stackblitz.com/edit/angular-mhtnsd Stackblitz: https://stackblitz.com/edit/angular-mhtnsd

Try like this:试试这样:

Working Demo 工作演示

<div class="col-md-3" id="leftNavBar">
  <ul *ngFor="let item of nestedjson">
    <li class="parentNav">parent1</li>
    <li class="childData">
      <ul>
        <li *ngFor="let child of item.value">{{child}}<span class="pull-right"><input type="checkbox"></span></li>
      </ul>
    </li>
  </ul>
</div>

The solution is the one below.解决方案如下。 Check stackblitz .检查stackblitz Your solution side by side with ngFor.您的解决方案与 ngFor 并列。

<div class="col-md-3" id="leftNavBar">
  <ul *ngFor="let parent of nestedJson">
    <li  class="parentNav">{{parent.name}}</li>
    <li class="childData">
      <ul>
        <li *ngFor="let child of parent.value" >{{child}}<span class="pull-right"><input type="checkbox"></span></li>
      </ul>
    </li>
  </ul>
</div>

If you have a JSON string, then you will need to use JSON.parse() to get the object in the format above.如果您有 JSON 字符串,那么您将需要使用 JSON.parse() 来获取上述格式的 object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM