How to retrieve data from a nested ng-repeat loop in AngularJS when the outer loop is a separate object

Is it possible to access the grandparent object of an ng-repeat item in angular?

I'm having trouble achieving this.

In the example below, I want to display the text of the parent object.

This is a simplified version; I am seeking a solution to make it function similarly to the top example or an explanation of why it won't work.

Below is the DOM structure:

<div ng-app ng-controller="MyCtrl">
 <!-- this is the one i cannot get working -->
  <div>
    <div class="copyData" ng-repeat="copy in copyDatabase">
    <div ng-repeat="header in masterHeaders">
      <div ng-repeat="test in copy.Translations">
        set 1: {{test.LanguageAbreviation}}
      </div>
  </div>

  <div>
  <!-- this one does work but i need the item from the masterHeaders -->
    <div class="copyData" ng-repeat="copy in copyDatabase">
    <!-- <div ng-repeat="header in masterHeaders"> -->
      <div ng-repeat="test in copy.Translations">
        set 2 : {{test.LanguageAbreviation}}
      </div>
      <!--</div>-->
    </div>
  </div>

Here is the JSON Object:

   function MyCtrl($scope) {
  $scope.copyDatabase = {
    "data": {
      "Language": "English",
      "Translations": [{
        "LanguageID": 308,
        "LanguageName": "Arabic - Libya",
        "LanguageAbreviation": "ar-LY",
        "AvailableLanguages": [{
          "ID": 308,
          "Name": "Arabic - Libya"
        }]
      }, {
        "LanguageID": 307,
        "LanguageName": "Arabic - Egypt",
        "LanguageAbreviation": "ar-EG",
        "AvailableLanguages": [{
          "ID": 307,
          "Name": "Arabic - Egypt"
        }]
      }]
    }
  }
}

I've created a JSFiddle for reference:

http://jsfiddle.net/u7hk0qvj/5/

Answer №1

After reviewing your jsfiddle, it appears that there is a fundamental error in the way Angular is recognizing your MyCtrl function. To rectify this issue, you must define MyCtrl as an angular component.

Currently, you have created a global function MyCtrl which is not operating within the angular context. Here is an example of how to properly declare it:

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

myApp.controller('GreetingController', ['$scope', function($scope) {
  $scope.greeting = 'Hello!';
}]);

For more information on defining controllers in Angular, please refer to this resource

Answer №2

In order to ensure the correct functionality, it is necessary to include the following:

$scope.masterHeaders = {
  "home": "home",
  "contact": "contact"
}

The object must also be present in the controller. If Angular cannot locate the object, it will not render any of the DOM elements within the ng-repeat directive.

For a demonstration, refer to this working fiddle:

http://jsfiddle.net/u7hk0qvj/8/

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

How AngularJS setValidity function can prevent the modelValue from updating

I've encountered a challenge with a form that I can't seem to figure out. Here's what's going on: To address the issue, I came across an interesting directive from this source: https://github.com/TheSharpieOne/angular-input-match This ...

A guide to resolving the issue of invalid objects as a React child in Nextjs13

Seeking help on resolving an issue in Nextjs13, where I'm encountering the error "objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead" The scenario involves fetching ...

Exploring the iteration of JSON objects within an array using AngularJS

My auto search module has the following JSON structure. I need to iterate through an array of JSON objects and use keys and values as required. I have attempted the code below. However, with the provided JSON object, I am able to retrieve the key but not ...

Changing the CSS class of the Bootstrap datetime picker when selecting the year

Is there a way to change the CSS style of the Bootstrap datetime picker control so that when selecting years, the color changes from blue to red? I attempted to do this with the following code: .selectYear { background-color:red!important; } However ...

Significant delay in fetching model data for controller via XHR

My controller is designed to make an ajax call to retrieve its model, which is a 4.5k json containing a 2d array. This data is then used to create a combo displaying a series of labels in the html below: <select data-ng-controller="CGSimpleXHRComboCont ...

Double Triggering Problem Arises During Partial Refresh

My dojo button bar is connected to a csjs function that performs a partialrefreshget() on a datable control. The datasource for the datatable control is a view. Within the this.keys property, I have implemented logic to check if the partialrefresh was ini ...

Can you explain the concept of System.register in a JavaScript file?

Can you explain the purpose of System.register in a JS file when utilizing directives in Angular 2? ...

What is the process for applying the source from an object while utilizing video-js?

I've been struggling to use a video player for streaming m3u8 videos. The code below works, but I want to dynamically set the source using an object. <video id="my-video" class="video-js" auto ...

What is the initial Validity setting in Angular?

When working on an Angular project, I encountered an issue where setting $setValidity to false caused some trouble. $scope.form[key].$setValidity("has_already_taken", false); I was unsure of how to reset the validity to true. I attempted using $scope.for ...

Items set to "flex" will not stretch, and when they are set to "inline-flex," it causes the other items within

My challenge lies in getting an input box to expand across the entire width of the screen. Unfortunately, due to the elements it is enclosed within, the input box only occupies a small portion of the screen. Moreover, when it expands, it also pulls the sur ...

Using Jquery to store input values from within <td> elements in an array

I'm trying to capture user input from a dynamically changing table with 2 columns. How can I retrieve the data from each column separately? The size of the table is adjusted by a slider that controls the number of rows. Below is the structure of my ta ...

Tips for configuring ng-model expressions for a dropdown menu

I am currently implementing Angular JS (1.6) and facing a challenge. I need to create four dropdown menus with the same values using a for loop in my template, which I have successfully accomplished. However, I now require setting a value for ng-model in o ...

Pattern for handling errors in an efficient and streamlined manner

Currently, I am exploring the use of Express for developing a basic JSON API. However, I have concerns about how to effectively manage input parameter validation and error handling in my project. Errors can occur during both the validation process and when ...

Implementing a sleek show/hide transition using slideToggle in jQuery

Trying to implement show/hide content with slideToggle and it's functioning, but the animation effect on the table is not smooth. Attempted two different codes, but none provided the desired animation effect: $('.more').slideToggle(' ...

Verification of data using PHP, MySQL, and jQuery

I'm currently working on a process to extract data from an Excel file and then pass it on to a PHP file for validation against a database. If the data doesn't exist in the database, a message will be displayed to notify the user of incorrect data ...

Animated smooth updates in d3 line graphs are the key to creating dynamic and

I'm attempting to modify an example of Animated Line Graphs from: http://bl.ocks.org/benjchristensen/1148374 <div id="graph1" class="aGraph" style="width:600px; height:60px;"></div> <script> function draw(id, width, height, upd ...

Sending data using jQuery to a web API

One thing on my mind: 1. Is it necessary for the names to match when transmitting data from client to my webapi controller? In case my model is structured like this: public class Donation { public string DonorType { get; set; } //etc } But the f ...

Generate a loop specifically designed to execute the code following the menu in the script

My website has the code snippet below: let txt_com = document.querySelector(".text_user"); let num_com_user = document.querySelector(".massage_for_user"); txt_com.addEventListener("click", function() { if (this.classList.contains("num_com_user")) { ...

Exploring the capabilities of utilizing Await/Async functions in Node.js

Here is my custom Axios Request function to make API calls. export function axiosGet (url) { return opsutils.get(url) .then(function (response) { return response.data.data; }) .catch(function (error) { return 'An error occu ...

What steps should I take to link form inputs from various child components to an array that is set in a parent component?

I'm in the process of linking form input from various child components (item-input-component) to an array itemList[] that is defined in a parent component (add-invoice-component). The goal is to gather three inputs (itemName, quantity, price), create ...