Creating a dynamic structure for output data in AngularJS requires incorporating various features of the framework to

As a beginner in angularJS, I am facing challenges in creating dynamic output for my simple app.

Below is the HTML code snippet -

<div ng-app="MyApp">
  <div ng-controller="TabsDemoCtrl">
    <div ng-controller="TabsDemoCtrl">  
      <h1> Struggling to display dynamic data like this?</h1>
      <table ng-repeat="customize in data">
        <tr>
          <th colspan="2">{{customize.product.name}}</th>
        </tr>
        <tr>
          <td colspan="2">Assets</td>
        </tr>
        <tr>
          <td>{{ How do I bind dynamic attribute data here? }}</td>
          <td>{{ How to bind dynamic subattribute data here? }}</td>
        </tr>
      </table>

    </div>
  </div>
</div>

And here's the JS code -

angular.module('MyApp', [
    'ui.bootstrap']);

(function(MyApp) {
  'use strict';
  MyApp.controller('TabsDemoCtrl', ['$scope', function($scope) {
    // categories
    $scope.data = [
    {
        product : {
            name  : 'Product 1'
        },
        assets : {
            color : {
                black : '/file/6d2ceb60-257b-47e6-9db0-3a2299ff75f2.png'
            }
        }
    },
    {
        product : {
            name  : 'Product 2'
        },
        assets : {
            soles : {
                black : '/file/840ec1ff-6d27-40af-b4ca-277aa09ad147.png',
                red : '/file/1970f2e2-b7a0-439c-98d9-b9ea1604c227.png'
            },
            material : {
                black : '/file/aebe8f68-60fd-4fda-bd46-00e80f190ba2.png',
                green : '/file/e225e20d-5b97-4a60-8337-0551064a8d8c.png'
            },
            lining : {
                blue : '/file/6d2ceb60-257b-47e6-9db0-3a2299ff75f2.png',
                red : '/file/280fecb5-ebe5-47cb-85f4-4d1bf6dd8ed0.png'
            }
        }
    }
];


  }]);
})(angular.module('MyApp'));

I attempted to create an output of dynamic structured data in this demonstration link, but unfortunately it didn't work.

Can anyone guide me on how to display this dynamic data in the view?

Answer №1

Here's a quick hint:

<li ng-repeat="(key,val) in product.assets"></li>

You can use the above code snippet to render all keys and values. Remember to check whether 'val' is an object or a string. If it's an object, you'll need to render another level of key-value pairs.


<div>
    <ul>
        <li ng-repeat="product in data">
            <p>{{product.product.name}}</p>
            <ul>
                <li ng-repeat="(key,val) in product.assets">
                    {{key}}
                    <ul>
                        <li ng-repeat="(key,val) in val">
                            {{key}} => {{val}}
                        </li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>
</div>

I recommend designing the template to display recursive data and performing data validation. Avoid using (key,val) template recursion on strings!

For more guidance, please refer to this: http://jsfiddle.net/brendanowen/uXbn6/8/

Answer №2

Click on this link http://example.com/edit/abc123 and see if it helps with your question.

Before parsing JSON data, double check that the format is accurate. You can use this online JSON parser tool:

https://jsonformatter.example.com/

If this resolves your problem, please let me know. Thank you!

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

The MongoDB GridFS is refusing to accept the buffer being written

Hey everyone, I've been working on this issue for nearly a day now and can't seem to figure it out. I'm utilizing multer's inMemory flag to upload an image from my website. My approach involves writing the buffer received from multer to ...

What are some techniques for monitoring an Angular promise chain with Jasmine?

Incorporating AngularJS, CoffeeScript, and Jasmine within WebStorm, my goal is to conduct unit testing on a chain of promises. Imagine having the subsequent example service: Angular Service class ExampleService stepData: [] constructor: (@$http) ...

Cannot Render JSON Array in Fetch Request with React and Express

My React client is attempting to utilize the 'fetch' api for a GET request to my node.js Express API. Despite enabling CORS and researching numerous questions, I'm unable to determine why the data isn't displaying on my localhost React ...

When trying to call a function inside jQuery's $.post, undefined is not recognized as an object

My attemptSearch function utilizes jQuery $.post to retrieve JSON results, but I'm encountering an error The error states: Undefined is not an object This error occurs on the call this.getSearchResults.bind(this) Interestingly, I have a simi ...

Implementing automatic page reloaded with Node.js and express-handlebars

I'm currently working on a Node.js and Express project that utilizes express-handlebars as the app's template engine. The application reads data from a MySQL database and presents it in a table format. To enable pagination, I've implemented ...

How can I prevent an iframe from scrolling by making changes to the linked page?

Is there a method to prevent the scrollwheel from functioning on a page that is being linked by an iframe? I am attempting to use an iframe to link to a website I created, but modifying the scrolling attribute of the iframe to "no" does not stop the scro ...

The Ajax request is failing to function properly

Check out this code snippet I have: var _07votes = $("#07votes"); $(document).ready(function() { setInterval(function() { $.ajax({ url: "http://services.runescape.com/m=poll/retro_ajax_result?callback=?", ...

How can I make a checkbox checked in AngularJS?

Hello, I am currently working on a web application using AngularJS. Within this application, I have a form where I am populating values into a multi-select dropdown. <li ng-repeat="p in locations"> <input type="checkbox" ng-checked="master" n ...

Issue connecting to the MongoDB server at 127.0.0.1:27017: Could not establish a connection

Encountering an Error When Trying to Run "mongo" on macOS: ➜ server git:(master) ✗ mongo MongoDB shell version v4.0.4 connecting to: mongodb://127.0.0.1:27017 2018-12-17T17:14:54.659-0800 E QUERY [js] Error: couldn't connect to server 127.0 ...

Is there a way to rigorously validate my HTML, CSS, and JavaScript files against specific standards?

Can modern browsers suppress errors in HTML, CSS, and JS sources? Is there a method to uncover all mistakes, no matter how small they may be? ...

Expanding the size of one div causes the surrounding divs to shift positions

I am currently working on creating a row of divs that expand when hovered over by the mouse. I have managed to achieve this functionality, but now I want the expanding div to cover its neighboring divs partially, without affecting their sizes or moving the ...

Are there any techniques for running unit tests on a Vue.js transition?

I have been working on a Vue component that includes a transition with a dynamically generated name. My challenge is to test whether the transition name is correctly set based on the props I pass into the component. Below is the code snippet of the compone ...

JQuery Challenge: Solving Dynamic Modal Issues

I'm in the process of creating a webpage that features multiple divs, each with its own unique image and title. Within each div, there's a button that, when clicked, should grab the specific image and title and display them in a modal. However, I ...

Whenever I attempt to import the "Highway" package, I encounter an error stating "Unexpected identifier."

After installing Highway through the terminal, I encountered an issue when running the script below: import Highway from '@dogstudio/highway'; import Fade from './transition'; const H = new Highway.core({ transition: { default: ...

Issue with AngularJS custom filter resulting in empty data output

Looking for help with applying a filter to data retrieved from Firebase. This is how my HTML appears: <span ng-bind-html="game.rating | ratings"></span> Note: Removing "| ratings" results in the original text being displayed from Firebase. ...

Change the colors and messages shown when the button is clicked or hovered over

I'm seeking assistance in getting this code to function properly, specifically to display different colors and messages when specific buttons are clicked or hovered over. My goals are: To change the background color of the page when the button &apo ...

Transmitting extensions via AJAX

I am having trouble sending navigator plugins with AJAX as I am only getting one plugin in the result. The plugin list currently shows: Shockwave Flash. However, it should display like this: Shockwave Flash - Chrome Remote Desktop Viewer - Native Client.. ...

Foundation 6 off-canvas-menu does not conceal the page that is currently visible to the user

I am currently working on a small viewport off-canvas menu using Foundation 6, but I am having trouble making it overlap the entire visible page area. The right-hand side underneath the off-canvas menu does not have background opacity. https://i.sstatic.n ...

Include a predetermined parameter for the upcoming callback

Currently, I am working on loading files asynchronously by utilizing d3's queue, defer, and await features. The issue arises when I attempt to execute this process in a loop, where for each iteration, I aim to store the retrieved data in a dictionary: ...

Issue with displaying Wavesurfer.js waveform in custom Shopify section

Greetings to the Stockoverflow Community, I am facing a challenge with integrating WaveSurfer.js into a customized section on my Shopify store. Even though I have successfully implemented the section, the waveform visualization is not appearing. Link to ...