Angular - Make nested list from JSON

soktinpk

I have a set of JSON data that I would like to display in a nested list:

The JSON comes in the following format:

["Item 1", "Item 2", "Item 3", ["Nested Item 1", "Nested Item 2"]]

The html should be:

<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
  <li>
    <ul>
      <li>Nested Item 1</li>
      <li>Nested Item 2</li>
    </ul>
  </li>
</ul>

I don't have control of the JSON, and it may be deeper than 2 levels.

Of course, I tried this:

<ul>
  <li ng-repeat="item in data">{{item}}</li>
</ul>

but it doesn't work because for nested items, it simply displays the json.

How can I achieve nested lists in AngularJs?

Fiddle: http://jsfiddle.net/m7ax7tsa/

soktinpk

I found a solution thanks to zsong and Blackhole.

Resulting HTML:

<div ng-app="testApp">
    <div ng-controller="TestCtrl">
    <script type="text/ng-template" id="/nestedList.html">
        <ul>
            <li ng-repeat="item in data">
                <div ng-switch="isString( item )">
                    <div ng-switch-when="true">{{item}}</div>
                    <div ng-switch-when="false">
                        <!-- Recursive template!! -->
                        <div ng-include="'/nestedList.html'" ng-init="data = item">
                        </div>
                    </div>
                </div>
            </li>
        </ul>
    </script>
    <div ng-include="'/nestedList.html'"></div>
    </div>
</div>

I used a recursive template which included itself. It borrows heavily on the answer of Unknown number of sublists with AngularJS. In addition, I had to add the following code to the controller:

$scope.isString = function (item) {
    return typeof item === "string";
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Nested JSON from adjacency list

From Dev

Flatten Pandas DataFrame from nested json list

From Dev

nested html list from json,csv

From Dev

Flatten Pandas DataFrame from nested json list

From Dev

Extract Values from nested Json list

From Dev

Grabbing value from nested json array with Angular

From Dev

How to make a dynamic form from JSON in angular?

From Dev

How to make a nested dictionary from a list's items?

From Dev

R Make Nested List From Data.Frame

From Dev

How to make nested JSON result from MySQL query in NodeJS?

From Dev

Build a Json file from python dictionary and nested dictionary/list

From Dev

Dynamic nested ul\li list from json data using Javascript

From Dev

Accessing JSON value from nested list in C#

From Dev

Create dynamic nested list from JSON using jQuery

From Dev

Make list of javascript objects from json and access these objects from their identity

From Dev

nested json in angular 2

From Dev

How can I pass a nested json object to rails from angular

From Dev

How can I pass a nested json object to rails from angular

From Dev

Make HTML List Using Nested JSON Response( Having Undefined Json Index Level)?

From Dev

How to make nested list operations?

From Dev

How to make nested list operations?

From Dev

How to fire a click event to make a selection from a dropdown list in Angular

From Dev

List from nested predicates

From Dev

replicate within list element to make nested list

From Dev

how to make view (form)from json using angular?

From Dev

make html tags render correctly from json in angular directive template

From Dev

how to make view (form)from json using angular?

From Dev

make html tags render correctly from json in angular directive template

From Dev

make JSON array from dynamic form data angular

Related Related

  1. 1

    Nested JSON from adjacency list

  2. 2

    Flatten Pandas DataFrame from nested json list

  3. 3

    nested html list from json,csv

  4. 4

    Flatten Pandas DataFrame from nested json list

  5. 5

    Extract Values from nested Json list

  6. 6

    Grabbing value from nested json array with Angular

  7. 7

    How to make a dynamic form from JSON in angular?

  8. 8

    How to make a nested dictionary from a list's items?

  9. 9

    R Make Nested List From Data.Frame

  10. 10

    How to make nested JSON result from MySQL query in NodeJS?

  11. 11

    Build a Json file from python dictionary and nested dictionary/list

  12. 12

    Dynamic nested ul\li list from json data using Javascript

  13. 13

    Accessing JSON value from nested list in C#

  14. 14

    Create dynamic nested list from JSON using jQuery

  15. 15

    Make list of javascript objects from json and access these objects from their identity

  16. 16

    nested json in angular 2

  17. 17

    How can I pass a nested json object to rails from angular

  18. 18

    How can I pass a nested json object to rails from angular

  19. 19

    Make HTML List Using Nested JSON Response( Having Undefined Json Index Level)?

  20. 20

    How to make nested list operations?

  21. 21

    How to make nested list operations?

  22. 22

    How to fire a click event to make a selection from a dropdown list in Angular

  23. 23

    List from nested predicates

  24. 24

    replicate within list element to make nested list

  25. 25

    how to make view (form)from json using angular?

  26. 26

    make html tags render correctly from json in angular directive template

  27. 27

    how to make view (form)from json using angular?

  28. 28

    make html tags render correctly from json in angular directive template

  29. 29

    make JSON array from dynamic form data angular

HotTag

Archive