Utilizing AngularJS ng-repeat with a two-dimensional array

Would it be possible to incorporate ng-repeat within a div like the following?

<li ng-repeat="image in HomeCtrl.images.data" >
    <a ng-href="@{{ image.code }}"><img ng-src="@{{ image.url }}" ></a>
</li>

Displayed below is the images array:

{
  "data": {
    "url": [
      "https://url1.com",
      "https://url2.com"
    ],
    "code": [
      "3cs14vs4",
      "512631va"
    ]
  }
}

Please note that the images array can be found inside HomeCtrl.

Answer №1

Ensure that the action is performed within a view already associated with the controller. If you are in a different controller, you can implement something similar to this:

<ul ng-conroller="HomeCtrl">
  <li ng-repeat="image in images.data.url track by $index" >
    <a ng-href="{{ images.data.code[$index] }}">
       <img ng-src="{{ image.data.url[$index] }}" >
    </a>
  </li>
</ul>
  • It's important to maintain the order of the elements for synchronization.

Regarding your query, I believe a more optimal data structure would look like this:

{
 {"code": "xx", "url": "yyy"},
 {"code": "xx", "url": "yyy"},
 {"code": "xx", "url": "yyy"},
 ....
}

Answer №2

Below is a functional code snippet for your reference.

(function(angular) {
  'use strict';
angular.module('app', [])
  .controller('ExampleController', ['$scope', function($scope) {
    //Modify the Json Object as needed
    $scope.images = { 
   "data":[
     {"url":  "https://url1.com","code":"3cs14vs4"},
     {"url":  "https://url2.com","code":"512631va"}
   ],
}  
    
    
 }]);
})(window.angular);
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8>
  <title>Example</title>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.js"></script>
  
</head>
<body ng-app="app">
  <div ng-controller="ExampleController">  
  
 <ul>
 <li ng-repeat="image in images.data" >
    <a ng-href="{{ image.code }}"><img ng-src="{{ image.url }}" ></a>
</li>
</ul>
    
</div>
</body>
</html>

Note: The object data structure has been modified for demonstration purposes.

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

Summernote - Add Text while maintaining formatting requirements

Currently, I am facing an issue where inserting a string of text from a dropdown list into the Summernote text editor results in the closing of formatting tags such as <'strong'>, <'p'>, and more. After inserting the string ...

One can activate an event once all templates and data have been fully loaded

Is there a way to trigger an event once all templates and data have finished loading in an AngularJS application? I've completed my app, but on the production server there seems to be a delay where the templates load first and then the data follow, le ...

Creating dynamic properties in a JavaScript object literal is a great way to customize

I am encountering an issue where I am trying to define input values from an HTML page as properties of a JavaScript Literal object, but I keep getting an error stating Undefined when I try to access it in the JS file. For example, if I have an input value ...

having difficulty iterating through the array in Perl

Having issues with my perl script: my %perMpPerMercHash; foreach my $sheet () { #proper ranges specified foreach my $row ( ) { #proper ranges specified #required variables declared. push(@{$perMpPerMercHash{join("-", $mercId, $mp ...

Merge two arrays of objects with underscore functions

Here are two arrays of objects that I have: var x = [{id:456, name:'falcon'}, {id:751, name:'gagarin'}, {id:56, name:'galileo'}] var y = [{id:751, weight:6700}, {id:456, weight:2958}, {id: ...

Angular resource integrated into a JSFiddle setup

Having a bit of trouble setting up my jsfiddle. I've been trying to add an Angular library but for some reason, it's not working in mine. My version - Not functioning Working example here Any ideas on what might be going wrong? (code below is ...

No element found with the specified exportAs value of "ngForm" on the <form> tag

I am currently experimenting with a template driven form in Angular, but I encountered an error stating **There is no directive with “exportAs” set to “ngForm"** I have made sure to import FormsModule and ReactiveFormsModule in app.module.ts as well ...

React Image Error Handling: How to deal with broken images in

I am currently working on a project where I need to retrieve the maxresdefault of various YouTube thumbnails. However, some YouTube videos do not have high-resolution thumbnail images available. To handle this scenario, I added an onError prop to the img ...

Tips on Extracting Data from a JSON Object with an Embedded Array

Check out this example of a Json Object: {"UserName":Mike,"IsActive":0,"ChbxIsActive":false,"MyAccountsAvailable":[{"Id":"157A","MyAccount":"CHRIS MCEL","MyCheckBox":false,"Tags":null},{"Id":"157B","MyAccount":"DAN BONE","MyCheckBox":false,"Tags":null} He ...

Looking for assistance in retrieving a document by its reference id from Firestore using ReactJS?

As a newcomer to using firestore and reactjs, I am currently working on creating a basic react code that retrieves documents from 2 different collections. The two collections I am focusing on are: user-habits {email, habitid} habits {habitid, conte ...

Interested in discovering the ins and outs of the JavaScript Map function?

Currently, I am delving into this JavaScript function: function solution (array, commands) { return commands.map (v => { return array.slice(v[0] -1, v[1]).sort((a, b) => a - b).slice(v[2] -1, v[2])[0]; }); } I am puzzled about th ...

Adapting the Three.js displacement map shader to incorporate all RGB values instead of solely relying on the red

My goal is to enhance the precision of the displacement map by using an rgba texture to display displacement values with 32 bits accuracy instead of the default 8 bits in three.js. I am working on visualizing a flooding scenario and need to observe minute ...

Implementing an AJAX search functionality in Symfony2

In the development of a basic CRUD application in Symfony2, I am looking to incorporate a search function on a specific page. The goal is to trigger a search query by entering text into an input field, which will then initiate an AJAX call. The response f ...

Tips for creating a high-performance div.innerHTML code similar to AngularJS

When I find myself needing to dynamically change a small piece of HTML in HTML before or after the success or error callback of an ajax request, I typically use: document.getElementById("xyz").innerHTML("<img />"); In jQuery, you can achieve the sa ...

While utilizing Ajax with Spring, it is possible to send a JavaScript object and receive it as a custom object. However, there was an issue with

One of my challenges in Java is working with a custom class that looks like this: public class AddressesVO { private Long addressId; private String address; public Long getAddressId() { return addressId; } public void setAddressId(Long addressId ...

`Is there a way for Javascript to retrieve information sent from Python Flask's render_template() method?`

Issue: I am facing difficulties in retrieving and displaying the data that I send from Javascript code when the user visits the site's landing page. The data in question is a dataframe. Backend Python Code: from flask import Flask, render_template, ...

Navigating to the next sibling child in Angular 1 using Ui-router transition

I'm trying to figure out how to effectively transition between sibling states using $state.go() in ui-router. Imagine a child state with integer-based URLs: /parent/1, parent/2, parent/3 and so on I want to be able to navigate between these child st ...

How can I utilize determinePreferredLanguage in angular-translate to configure based on user language preference?

Within our web application, each user account includes a language setting. This is crucial for ensuring that certain email communications sent from the server side are translated based on the user's language preference. Yet, we are interested in util ...

Generate an image from HTML code

Is there a method to convert HTML into an image format such as PNG? I am aware this can be achieved using canvas, but I am interested in rendering regular HTML elements like divs. Could you suggest a way to accomplish this? ...

Best way to position camera to look at or above a specific location on a sphere in Three.js

Is there anyone out there with a suggestion on how to position the camera so it is directly facing upwards towards a specific point on the sphere? LATEST DEVELOPMENT It turns out that Radio's solution was spot on. Upon further investigation, I disco ...