Iterating through an array within another array using ng-repeat

Hey there! I'm new to working with angular js and have been exploring the documentation on the official site. I attempted to implement a solution, but unfortunately, no data was displayed. Can someone point out what I may be doing incorrectly?

This is the array I am working with:

$scope.newArray = [{
    name: 'Robert',
    chemical: [{
        chem_status:"Active",
        chem_code:"0098A"
    }],
    place: 'London'
},
{
    name: 'Andy',
    chemical: [{
        chem_status:"Active",
        chem_code:"0098A"
    }],
    place: 'London'
}];

Below is a snippet of my html code:

<tr role="row" ng-repeat="chemical in newArray.chemical by $index">
    <td class="sorting_1">{{chemical.chem_status}}</td>

Answer №1

angular.module("app", [])
  .controller("sampleController", function($scope) {

    $scope.newArray = [{
        name: 'Alice',
        chemical: [{
            chem_status: "Active",
            chem_code: "0101A"
          },
          {
            chem_status: "Inactive",
            chem_code: "010"
          }
        ],
        place: 'USA'
      },
      {
        name: 'Alice 2',
        chemical: [{
          chem_status: "Active",
          chem_code: "0101B"
        }],
        place: 'France'
      }
    ];

  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app">

  <div data-ng-controller="sampleController">

    <div data-ng-repeat="item in newArray track by $index">
      <div>Name: {{item.name}}</div>
      <div>Place: {{item.place}} </div>
      
       <div style="padding-left:20px" data-ng-repeat="chem in item.chemical track by $index">
        <div>Status: {{chem.chem_status}}</div>
        <div>Code: {{chem.chem_code}}</div>
        <hr/>
      <div>

    </div>

  </div>
</div>

Answer №2

Here's a suggestion for you

<div ng-repeat="elements in updatedArray.element by $index">
   <tr role="row" ng-repeat="element in elements by $index">
            <td class="sorting_1">{{element.status}}</td>

Answer №3

Modify the initial chemical

<tr role="row" ng-repeat="newchemical in newArray.chemical by $index">
            <td class="sorting_1">{{newchemical.chem_status}}</td>

Answer №4

When looking at the code above, it is important to note that $scope.newArray is actually an array itself. This means that we must first iterate over the outer array before iterating over the inner one.

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

//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});

myApp.controller("MyCtrl",function($scope) {
    $scope.name = [{
          "name": "Robert",
          "chemical": [{
          "chem_status":"Active",
          "chem_code":"0098A"
          }],
          "place": "London"
          }];
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-App="myApp" ng-controller="MyCtrl">
  Hello,
  <table>
    <tr ng-repeat="chemicalName in name">
            <td class="sorting_1">
              <span ng-repeat="status in chemicalName.chemical">      {{status.chem_status}}
              </span>
            </td>
            </tr>
  </table>
</div>

This approach will yield optimal results.

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

Guide to incorporating multiple rows from SQL into a PHP array

I currently have the variable $view_data['auth_info'] which is filled with a query row and is functioning correctly. However, I am now trying to include an additional row in this array. Is there a way to achieve this without encountering any conf ...

Challenge encountered while attempting to implement grid system in ionic framework

I'm completely new to the world of ionic framework, and I really need some assistance in completing this view. Please see the image reference https://i.stack.imgur.com/WOBFw.png I have made an attempt at it with the following code: <div class ...

Vue Application experiences issues resizing Three.js Animation in Canvas

Currently, I am deep diving into learning Three.js within a Vue.js component. Take a look at my code snippet: <script> import * as THREE from "three"; export default { name: "Scene", mounted() { this.initializeThree(); ...

Customize the background color in VueJS based on user roles

I am currently working on a vuejs project with different user levels. In the "user" level (and public area), there is a background image, while in the "admin" level, a plain white background is displayed. I have read that using style tags in the template ...

handling component interaction with react-redux store

Currently, I am in the process of developing my first significant project using react-redux. While trying to establish state mapping between components in react-redux, I seem to have missed a crucial step. Almost everything is functioning smoothly except ...

Inject the content into the DIV tag retrieved from the source

I am working on a website layout that consists of a left panel and a content div on the right. Currently, when a user clicks on a link in the left panel, a JavaScript Ajax call is made to load the corresponding content into the div tag named 'content& ...

What is the best way to find my way to a particular category within angular?

I'm currently in the process of learning Angular, but I am struggling with a particular issue. My focus right now is on developing an Angular ecommerce application. The REST API that has been set up seems to be quite simplistic. Here's what the J ...

Activate the submit button using jQuery

I have tested the code snippet that I shared on fiddle. I am looking to activate the save button by hitting the enter key, which will submit the form using ajax. The activation should occur when there is text content greater than 0 in the span. $(docum ...

Enhancing link functionality with jQuery on a dynamically generated server page

I am facing an issue with my navigation menu that includes dropdowns. On desktop, the parent items need to be clickable as well, which is not a problem. However, for it to be responsive on mobile devices, I need to account for the lack of hover capability. ...

Troubleshooting Problems with Wordpress Shortcode Arrays

Here is an example of how a shortcode appears: [posts3col ids="249, 318, 93" category="Events"] Below is the code related to the shortcode: add_shortcode('posts3col', 'posts_func'); function posts_func($atts){ extract(shortcode_a ...

switch out asterisk on innerhtml using javascript

Is there a way to replace the asterisks with a blank ("") in the innerHTML using JavaScript? I've attempted this method: document.getElementById("lab").innerHTML = document.getElementById("lab").innerHTML.replace(/&#42;/g, ''); I also ...

Adjust the log level on winston in real-time

Is there a way to update the log level dynamically in winston and have it reflect across multiple files? I am facing an issue where changing the logger level in index.js does not affect readfile.js. How can I resolve this? Below is the code snippet: win ...

Using AJAX to retrieve version information via jQuery

I have a straightforward script that extracts the value of a textarea and sends it via AJAX. When I input "??" as the text, the output seems to be corrupted with strange values. However, when I log out the retrieved value before sending it, everything appe ...

"Facing an issue with Google Chrome not refreshing the latest options in an HTML select dropdown list

Having trouble creating an offline HTML file that incorporates jQuery for the script. The page features a state select menu followed by a second menu for counties. When a state is selected, only the corresponding counties should display while others remai ...

Encountering a connection error when trying to access a Google spreadsheet within a Next.js application

I am currently exploring Next.js and attempting to utilize Google Sheets as a database for my project. Although my application is functioning correctly, there is still an error message displaying that says "not forgot to setup environment variable". I have ...

Utilizing Vue to Transform Unicode Characters into HTML Elements

I have a scenario where an API response contains a Unicode string that needs to be displayed correctly in the view. Despite Vue treating it as a string and not automatically converting it to HTML, I believe JavaScript should handle the rendering process. ...

Issues with closing Foundation 6 off-canvas feature

Here is a link that I have: If you click on the skip button in the middle, it will take you to the next slide where Foundation 6 off-canvas is implemented. The header containing the toggle button for the off-canvas menu is located outside of the menu its ...

Alternative method for handling web requests in case JavaScript is not enabled

Issue: i) I am developing a JSF2 application and need to implement a tab control on a page. When a user clicks on a tab, the content for the panel below should be loaded from an xhtml file on the server using an ajax call. ii) I want this functionality ...

Utilize the input field value in an HTML dialog box to assign it to a variable in the code.gs file with the help

I'm trying to create a dialog box where the user can select a year, and then have the server process the selected value using the function doSomethingWithCompetitionYear(theYear). I've researched several discussions, but I'm having trouble ...

The requested file cannot be accessed using XMLHttpRequest.open url

Currently, I am in the process of learning about AJAX by following the MDN tutorial. However, I have encountered an issue with the first sample code provided to fetch test.html. Regardless of whether I use absolute or relative paths, my local server consis ...