Displaying items in pairs in AngularJS with ng-repeat

The design I want to achieve is as follows:

<div>
    <div class="row">
        <div></div>
        <div></div>
    </div>
    <div class="row">
        <div></div>
        <div></div>
    </div>
    <div class="row">
        <div></div>
        <div></div>
    </div>
</div>

While this code generates the divs, I'm struggling with adding separators between each pair of items. I believe Angular should provide a simple solution for this.

<div>
    <div class="row">
        <div ng-repeat="object in objects"></div>
    </div>
</div>

Answer №1

If you're looking to tackle this challenge, consider leveraging a touch of javascript alongside ng-repeat as shown below:

<div>
    <div class="row" ng-repeat="array in obj2">
        <div ng-repeat="object in array">{{object}}</div>
    </div>
</div>

Next step is to establish a new object named obj2 within your angular controller using the objects array as demonstrated here:

$scope.obj2 = [];
while ($scope.objects.length) {
    $scope.obj2.push($scope.objects.splice(0, 2))
}

For an interactive demonstration, check out this Fiddle

Answer №2

If you understand what you need to accomplish, one way to do it is by utilizing the $index property provided by the ngRepeat directive and then applying a modulo operation on that index like so:

<div ng-repeat="object in objects">
    <div>My Content</div>
    <div data-ng-show="$index % 3 == 0">My Separator</div>
</div>

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

Configure webpack to source the JavaScript file locally instead of fetching it through HTTP

Using webpack.config.js to fetch remote js for Module Federation. plugins: [ new ModuleFederationPlugin({ remotes: { 'mfe1': "mfe1@http://xxxxxxxxxx.com/remoteEntry.js" } }) ], Is it possible to incorporate a local JS ...

Unit tests may not properly update the Angular Async pipe

Struggling with updating Observables using the async pipe in HTML during unit tests. I want to test not only the component itself but also ensure that child components are rendered correctly with the right Inputs. Here is a simple example where the issue ...

Troubleshooting: Discord bot failing to initialize on Heroku

After successfully hosting my bot on Heroku for a while, I temporarily switched back to self-hosting to update to discord.js v13. Now that I'm done with the updates and trying to re-host the bot on Heroku, I'm encountering this error: (node:4) Un ...

Preview and enlarge images similar to the way Firefox allows you to do

When you view an image in Firefox, it is displayed with the following characteristics: Centered within the browser window. Has a height/width that fits within the browser dimensions. Does not resize larger than its maximum size. Remains the same size if ...

The dreaded "fatal error: JavaScript heap out of memory" message struck once again while using npx

My attempt at setting up a boilerplate for a React app involved using the command npx create-react-app assessment D:\React>create-react-app assessment Creating a new React app in D:\React\assessment. Installing packages. This might take ...

Just delving into React for the first time and encountering undefined values in PropTypes

I am completely new to React and attempting to create a basic weather application in order to understand how components interact with each other. I have a forecast.json file that contains information such as temperature, date, humidity, wind speed, wind di ...

Ways to manage properties that are non-existent

Whenever vm8 encounters a property that doesn't exist, it will display an error message like this: Cannot read property 'value' of null. For example, when passing an id that doesn't exist: var pass = document.getElementById('pass ...

Exploring the concept of front-end deletion through ajax technology

I'm currently tackling a front-end CRUD RESTful API project and encountering challenges specifically related to the DELETE and PUT methods. While I have successfully managed to implement the GET and POST functionalities, utilizing a forms.html file f ...

Showcase -solely one property object- from numerous property objects enclosed within an array

Hello, I am seeking assistance as I have recently begun learning about angularJS. I am working with objects that have keys such as: scope.eventList=[]; for(){ var event = { id : hash, ...

The child_process module in Typescript is unable to recognize execSync as a valid function and returns

Currently, I am attempting to utilize the execSync function from the child_process module. However, after importing the module: /// <reference path="../../../../GENERAL/d.ts/node/node.d.ts" /> var execSync = require("child_process").execSync; Upon ...

High RAM consumption with the jQuery Vegas plugin

I've been scouring the internet, but it seems like I'm the only one facing this issue. Currently, I'm working on a website that uses the jQuery vegas plugin. However, I noticed that if I leave the page open while testing and developing, my ...

A comprehensive guide to utilizing the GROUP BY YEAR(created_at) function in sequelize

How can I effectively use the GROUP BY clause in sequelize for MySQL to group by year of creation? I attempted the following code but encountered an error: const result = await arbcase.findAll({ attributes: [[arbcase.sequelize.literal(`COUNT(*)`), "co ...

Use jQuery's .replaceWith method only once for each instance

Encountering a problem with my code that involves jQuery, and I'm struggling to find a solution. $("a[href=##]").replaceWith("<label>" + $("a[href=##]").text() + "</label>"); The issue arises when I have multiple links with the same href ...

AngularJS employs the identical service as the model but with distinct data

As I develop a factory service that offers several functions, here's an example of how it could be structured: var myApp = angular.module('panelServices', ['ngResource']); myApp.factory('myService', [...]{ function m ...

Get the latest html content and save it as a .html file using javascript or jQuery

Looking for a way to save an HTML page as a .html file? Having some trouble with jQuery modifications not being included in the exported file? Check out the code snippet below and let me know if you can spot what's going wrong! I'm still getting ...

Significant issue identified with ajax functionality, requiring immediate attention

After executing the code in the console, I expected to see the ajax object with a readyState of 0 (as I aborted the ajax call). However, Chrome is displaying the number 4 in the console instead of 0. This discrepancy is surprising. http://jsfiddle.net/8yC ...

Obtain JSX content from a React Component that has been imported

As part of our library documentation efforts, I am looking to convert a React Component function into JSX format. To illustrate, let's consider a rendered Button component and showcase the code used to create it. One approach could be to simply read ...

Alter the class of the div element every three seconds

Greetings, I trust everyone is doing well. I am in need of some assistance from your brilliant minds. I have a circular div along with three CSS classes, and my objective is to switch the div's class and update the label text every 3 seconds. Any insi ...

Is it possible to utilize jQuery to insert a chosen form into a table?

Here is the code example I am working with: <label for="">Select Ticker: </label> <select class="form-control" style="display: inline" id='selected'> {% for p in tickers %} <option value="{{ p.tick ...

Choose the span element within every other td that is not enclosed by an anchor tag

The table structure I am working with is as follows: <tr> <td> <a> <span> some content </span> </a> </td> <!-- td having straight span --> <td> <span> ...