Tips for filling <li> with data using ng-repeat from JSON

Working with AngularJS is a new experience for me, and I'm currently attempting to populate a dropdown list using JSON and ng-repeat. While I found ng-option for "select", I couldn't find a similar solution for using "li" elements.

Here is a snippet of the JSON file I'm trying to work with:

[
{
    "header": {
        "name": "Electronics",
        "subHeader": {
            "childHeader": [{
                    "name": "Mobiles",
                    "view": "#mobile"
                }, {
                    "name": "Tablet",
                    "view": "#tablet"
                }, {
                    "name": "Television",
                    "view": "#television"
                }, {
                    "name": "Headphones",
                    "view": "#headphones"
                }]
        }
    },
        "name": "Men",
        "subHeader": {
            "childHeader": [{
                    "name": "Shirts",
                    "view": "#shirts"
                }, {
                    "name": "T-Shirts",
                    "view": "#tshirts"
                }, {
                    "name": "Jeans",
                    "view": "#jeans"
                }, {
                    "name": "Trousers",
                    "view": "#trousers"
                }]
        }
    }

]

The JavaScript file that I'm currently using looks like this:

var mainAngular = angular.module('myApp', ['ngRoute']);
mainAngular.controller('headerCategoryController', function($scope,$http) {
var hCat=  "./customAngular/headerCatalogue.json";
$http.get(hCat).success(function(response){
    $scope.categories=response;
});
});

My desired output format is as follows:

            <div class="headerMenu" >
                    <ul class="nav navbar-nav">
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Electronics<b class="caret"></b></a>
                            <ul class="dropdown-menu">
                                <li><a href="index_category.htm">Mobiles</a></li>
                                <li><a href="product.htm">Tablets</a></li>
                                <li><a href="cart.htm">Televisions</a></li>
                                <li><a href="checkout.htm">Headphones</a></li>
                            </ul>
                        </li>
                    </ul>
                    <ul class="nav navbar-nav">
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Men<b class="caret"></b></a>
                            <ul class="dropdown-menu">
                                <li><a href="index_category.htm">Shirts</a></li>
                                <li><a href="product.htm">T-Shirts</a></li>
                                <li><a href="cart.htm">Trousers</a></li>
                                <li><a href="checkout.htm">Jeans</a></li>
                            </ul>
                        </li>
                    </ul>
                    </div>

Currently, I have the following implementation:

<div class="headerMenu" ng-controller="headerCategoryController">
                    <ul class="nav navbar-nav" ng-repeat="category in categories">
                        <li class="dropdown">
                            <a href="" class="dropdown-toggle" data-toggle="dropdown">{{category.header.name}}<b class="caret"></b></a>
                            <ul class="dropdown-menu">
                                <li ng-repeat="val in category"><a href="{{val.header.subHeader.childHeader.view}}">{{val.header.subHeader.childHeader.name}}</a></li>
                            </ul>
                        </li>
                    </ul>
</div>

I have been following the "json array disply in <li> using angular js" guide, but I'm still facing some challenges. Any help would be greatly appreciated!

Answer №1

I have made some corrections to the JSON you provided:

[
    {
        "name": "Electronics",
        "subHeader": [
            {
                "name": "Mobiles",
                "view": "#mobile"
            },
            {
                "name": "Tablet",
                "view": "#tablet"
            },
            {
                "name": "Television",
                "view": "#television"
            },
            {
                "name": "Headphones",
                "view": "#headphones"
            }
        ]
    },
    {
        "name": "Men",
        "subHeader": [
            {
                "name": "Shirts",
                "view": "#shirts"
            },
            {
                "name": "T-shirts",
                "view": "#tshirts"
            },
            {
                "name": "Trousers",
                "view": "#trousers"
            },
            {
                "name": "Jeans",
                "view": "#jeans"
            }
        ]
    }
]

Take note that both the headers and subheaders are now arrays.

Furthermore, the view section needed some adjustments as well:

<div class="headerMenu" ng-controller="headerCategoryController">
  <ul class="nav navbar-nav" ng-repeat="category in categories">
    <li class="dropdown">
      <a href="" class="dropdown-toggle" data-toggle="dropdown">{{category.name}}<b class="caret"></b></a>
      <ul class="dropdown-menu">
        <li ng-repeat="subheader in category.subHeader"><a ng-href="{{subheader.view}}">{{subheader.name}}</a></li>
      </ul>
    </li>
  </ul>
</div>

You can view a working example on Plnkr: http://plnkr.co/edit/9UN6DjEuSZCtBjc1SOhK?p=preview

Answer №2

In case you need a drop-down menu, you can achieve it by following these steps:

<select ng-options="optionslist">
   <option value="">All</option>
</select>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using conditional statements like 'if' and 'else' in JavaScript can

Can someone help me with solving my problem using if-else statements in Javascript? I need to filter names by gender and save them as keys - woman / man in local storage. Any assistance would be greatly appreciated. I am struggling to figure out how to im ...

How can I create a new div element for every item in an array by utilizing JSX?

I am currently working on creating a div element for each item in an array using reactjs JSX. I wasn't sure how to tackle this, so I attempted the following approach: {results.map((element, index) => ( <div key={index} className={ ...

What is the best way to align the 'Typography' component in the center?

Attempting to center it using flexbox with justify-content did not yield the desired result. I also tried replacing the Typography component with a div tag, but still no success. import {AppBar,Zoom,Toolbar,Typography,CssBaseline,useScrollTrigger,Fab,ma ...

Identify and click on the child span within the li element using Cypress

I am struggling to select a li element and click/check on the span with the checkbox class, but I can't seem to make it work. Here is what I have attempted: <div id="list-widget"> <ul class="tree"> <li class ...

Is there a way to eliminate the auto-opening feature of WordPress Shortcode Ultimate Accordion on mobile devices with the help of jquery

Currently, I am utilizing the Accordion feature of Wordpress Shortcode Ultimate plugin. Although the plugin does offer an option to open the accordion on page load, I would like to have them closed by default on mobile devices. How can I achieve this usin ...

What are some effective techniques to optimize this node.js code and eliminate redundancy?

I am currently in the process of setting up a basic template for my demonstration project and writing my first node.js program. The piece of code below is functioning properly for my initial test, but it contains duplicated sections - Getting connection, E ...

What is the method to retrieve content from a different domain using .load()?

When I try to retrieve data from a different domain using .load() or other jQuery ajax functions, it doesn't seem to work. However, accessing URLs on my own domain works perfectly fine. I've heard about a workaround involving PHP and setting up ...

Updating state in React using the spread operator is not allowed

As a newcomer to React, I've been struggling with using the spread operator to push new elements into an array stored in the state. My aim is to create an array with a sequence of different numbers. Here's the code snippet that I've been wor ...

Tips for configuring a schema within the `node_modules` directory using `$schema`

Currently developing an npm package inclusive of a schema file named set.schema.json. My query revolves around how I can designate this as the $schema for a JSON file within a separate project that has this particular package integrated as a dependency. ...

Utilizing ngCookies within an Ionic app

I am having an issue incorporating ngCookies into my project. Despite including the angular cookies library in my index.html file after ionic.bundle, the $cookies service seems to be pointing to an empty object when I try to access its functions in my code ...

Retrieve JSON information from External document through Ajax

I'm trying to incorporate an external JS file that contains some sample JSON code. However, I'm encountering an error when I include the sample JSON code in the file, specifically at the ":". Interestingly, when I validate the code using online t ...

Guide to merging two endpoints in express.js to create a single endpoint

I currently have 2 existing endpoints named /balance and /transactions. What is the most effective approach to create a new endpoint called /balance-and-transactions without having to refactor the existing code? For example: a('/balance', () =&g ...

There are certain lines of JavaScript/Node.js code that are failing to execute

app.get is not being executed. I have also attempted to include app.listen(3000). My goal is to retrieve the parameter passed from the first web page. This code is designed to fetch parameters sent by another web page and then construct a MySQL query and ...

UI-Select fails to display the already selected value

Does anyone have a solution for binding a UI-Select control to a Class[] list? The filling up part works fine, but I'm having trouble getting the selected item to display. Any suggestions on how to fix this? Below is my ui-select code: <ui-select ...

Calling all available data search specialists! Your expertise is needed for

Currently, I have a requirement to implement a name search functionality where the user can scroll through the results. The current implementation uses a query string in a GET call for searching, but I need to switch this to a POST method instead. Below a ...

Retrieve data from MySQL using a count query and convert the results into a JSON format

In my table, there is a column with datetimes and corresponding IDs. +----+---------------------+ | id | datetime | +----+---------------------+ | 0 | 2016-09-02 12:13:13 | | 1 | 2016-09-02 10:16:11 | | 2 | 2016-09-05 11:03:23 | | 3 | 2016- ...

Steps for setting a default option with a value of -1 and ensuring it is pre-selected in AngularJS

When using a select with ng-options, the selected option may have a value of '?'. <select ng-model="somethingHere" ng-options="option.value as option.name for option in options" class="ng-pristine ng-valid"> <opt ...

In Vue.js, it is not possible to modify a component variable within a callback function

I have implemented a custom Login.vue component using vue.js to allow users to log in to my application through AWS Cognito. The authentication process is handled by the ___.authenticateUser() method, which initiates a session with Cognito. Below is the co ...

AJAX - Self-Executing Anonymous Function

I have a question that may seem trivial, but I want to make sure I'm heading in the right direction. I've created two different versions of an XMLHttpRequest wrapper, and both are functioning correctly. const httpRequest = function () { let ...

Implementing a click event listener on an iframe that has been dynamically generated within another iframe

Below is the code I used to attach a click event to an iframe: $("#myframe").load(function() { $(this.contentWindow.document).on('click', function() { alert("It's working properly"); }); }) Everything seems to be working co ...