Displaying a two-dimensional array from a JSON file using AngularJS ng-repeat

Looking at this HTML file, I am trying to display a 2D array from the json-$scope.listOfIngredient

<div class="ingredientMapping" ng-repeat="IngredientMapping in listOfIngredient track by $index">

      <ul>
        <!-- BEGIN: Inner ngRepeat. -->
        <li  ng-repeat="x in IngredientMapping.Ingredient">

            {{x.name}}

        </li>
        <!-- END: Inner ngRepeat. -->
    </ul>
      </div>

Now, let's take a look at my Controller.js

var myApp = angular.module('myApp', []);

myApp.controller('mainController',function ($scope, $http) {

$scope.listOfRecipe = null;
    var url = "http://164.132.196.117/chop_dev/recipe/recipes.json";
    var url2 = "http://164.132.196.117/chop_dev/recipe/recipes/view/";
$http({
method: 'GET',
url: url

}).then(function (response) {
         $scope.listOfRecipe = response.data.recipes;

         var temp;
         for(var i=0; i<response.data.recipes.length;i++){
             $http({
                        method: 'GET',
                        url: url2+ response.data.recipes[i].Recipe.id +".json"
                    }).then(function (response2) {

                        $scope.listOfIngredient = new Array(100);
                        for (var j = 0 ;j<100;j++)
                        {
                            $scope.listOfIngredient[i] = new Array(100);
                        }
                        for (var j = 0 ;j<response2.data.recipe.IngredientMapping.length;j++)
                        {
                            $scope.listOfIngredient[i][j] = response2.data.recipe.IngredientMapping[i];
                        }

                        console.log($scope.listOfIngredient);

                    });
         }



     })

This is the link to my Json file

In implementing this, an error occurred and it seems to stem from $scope.listOfIngredient[i][j] = response2.data.recipe.IngredientMapping

I'm uncertain if I am doing it right when trying to get a 2D array in this manner

Answer №1

It appears that your mapping needs adjustment. Here is a solution that worked for me:

<div ng-controller="MyCtrl">
  <div class="ingredientMapping" ng-repeat="recipes in listOfIngredient">
    <ul>
      <li  ng-repeat="x in recipes.recipe.IngredientMapping">
        {{x.Ingredient.name}}
      </li>
    </ul>
  </div>
</div>

http://jsfiddle.net/Lvc0u55v/7841/

Edit:

so:

<li  ng-repeat="x in recipes.recipe.IngredientMapping">
instead of

<li  ng-repeat="x in IngredientMapping.Ingredient">

and:

{{x.Ingredient.name}} instead of

{{x.name}}

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

Chrome runs requestAnimFrame at a smooth 60 frames per second, while Firefox struggles to keep up at

I recently developed a canvas animation using requestAnimFrame and saw great results in Chrome. However, when I viewed it in Firefox, it seemed like a slideshow was running instead of a smooth animation. I'm unsure what could be causing this issue. F ...

Having trouble showing a specific number of JSON data items in SwiftUI

I am currently utilizing the SwiftUI framework and fetching JSON data from an Online API. After successfully decoding the data, I find myself facing a challenge in displaying only 6 random items from the decoded data. The requirement is to show these 6 ite ...

The dependency graph of npm modules shows significant differences

I've been exploring the capabilities of the npm-remote-ls package to analyze dependency trees for modules. This tool is installed globally on my system. When I execute Command 1: npm-remote-ls object-assign The tree structure displayed is as follows ...

Extract data from a deeply structured JSON object

Here is the structure of my JSON data: "{\"total_entries\":2,\"current_page\":1,\"total_pages\":1,\"per_page\":2,\"items\":[{\"workspace\":{\"background_color\":\"#ffedd3\",&b ...

Error message: Error in jQuery: Object is required for Internet

I have a button that is designed to trigger the opening of a jQuery UI Dialog when clicked. Strangely, it works perfectly in FF3, FF4, Chrome, and IE8 with ChromeFrame, but fails to function in regular IE8. The error message displayed simply states "Object ...

Using an Ajax call with Spring MVC may result in receiving plain text instead of JSON

This is the AJAX call that I am using: $.ajax({ url: 'configuration/activePlatform/', type: 'GET', dataType: 'json', contentType: "application/json", success: function(data){ ...

Transforming a jsonObject into a JavaScript array

I am working with a JSON object and I need to extract data from it to create an array. Here is an example of the desired array: ['city 1','city 2','city ..'] Below is the JSON output: {"\u0000*\u0000_data":[{"id": ...

Display a pop-up window upon clicking anywhere on the webpage using jQuery and HTML

Is it possible for me to create a pop-up window that opens a specific website when a user clicks anywhere on the page, similar to pop-up companies? Can this be achieved using HTML, JavaScript, and jQuery? Any help would be greatly appreciated. ...

In the PHP code, the value is calculated by adding three array keys together

I Possess 3 Arrays First Array : Array ( [0] => 0 [1] => 256 [2] => 512 [3] => 768 [4] => 1024 [5] => 1280 [6] => 1536 [7] => 1792 [8] => 2048 [9] => 2304 [10] => 2560 [11] => 2816 [12] => 3072 [13] => 3328 [ ...

Supply a JSON parameter as a variable into the .load() function

When a button is clicked, I am attempting to load a page using the .load() method with a POST request. The URL parameters are generated and displayed as JSON formatted in the button attribute btn-url. Problem: The parameter is not being passed to the .loa ...

Can you provide instructions on how to configure the npm settings to use the current directory in a pre

I'm currently working on setting the installation directory from where the user is installing to an npm config variable. This will help me reference it in my installation script. Can anyone advise if there is a command line solution for this or will ...

Watching a service's attribute from within a route in the EmberJS framework

There seems to be a concept that I'm struggling to grasp here. To my understanding, any instance of Ember.object should be able to observe properties on another instance of Ember.object. In my scenario, there is a service, a router, and a component i ...

Place an IconButton component next to each Material UI TableRow

I am trying to include an icon next to the material UI table row component. Similar to the hint icon shown in the screenshot below Here is my attempt so far, but it's not functioning as expected: Check out the code on CodeSandbox https://i.stack.i ...

Is there a way to retrieve real-time information using momentjs in a Vue.js application without needing to refresh the

I have a unique table where I showcase details about a particular website. Among the displayed information is the timestamp indicating when the data was last updated. At the top of the table, my aim is to include an icon that will only appear if the dura ...

CKEditor directive in AngularJS does not properly enforce the maxlength attribute in textarea

I am currently working on an AngularJS application with the CKEditor plugin. I have created a directive for CKEditor and everything seems to be functioning properly. However, I am facing an issue where I need to limit the character length to 50. I tried us ...

Having trouble retrieving data from getters in the Vue store while inside a template

Within my component, I have set up computed properties that are supposed to display text for blopp and blipp. However, while blopp is working fine, blipp is not showing anything. The end goal is to have blipp generate a list of strings from the store state ...

HTML - one div's child element takes precedence over another div

My HTML page features two consecutive divs. The first div contains a child span that has a relative position, causing it to overlap the second div. I have implemented click events for both divs. However, when I click on the span that overlaps the second d ...

Difficulty with Bootstrap 4 mobile navbar dropdown feature

<div class="baslik baslik1 baslik2 "> <nav class="navbar bg-light navbar-light navbar-expand-sm sticky-top "> <a href="./index.html" class="navbar-brand"><img src="img/512x512logo.png" ...

Utilizing Typeahead for Autocomplete in Durandal: A Step-by-Step Guide

I am attempting to implement an autocomplete input field with typeahead (Twitter Bootstrap) in a modal, but I am encountering difficulties making it function properly. Additionally, this autocomplete field needs to be observable with Knockout so that selec ...

Differences between using "break + return" and simply "return" in

Currently, I am developing a nodejs Heap and ensuring top performance is key. The code snippet I have looks like this: while(true) if(x) do something return if(y) do ... return if(z) do ... else return Suggestions were made to me to incorporate ...