AngularJS: Implement ng-repeat directive to generate two separate HTML containers

I have a collection of items and I want to divide them into two separate containers.

Is there a way to achieve this using ng-repeat?

<div>
    <ul>
        <li>Some item</li>
        <li>Some item</li>
        <li>Some item</li>
        <li>Some item</li>
        <li>Some item</li>
        <li>Some item</li>
    </ul>
</div>
<div>
    <ul>
        <li>Some item</li>
        <li>Some item</li>
        <li>Some item</li>
        <li>Some item</li>
        <li>Some item</li>
        <li>Some item</li>
    </ul>
</div>

Answer №1

Method 1 : utilize the slice method on the collection

We can utilize the slice method on the collection :

<div ng-app="myApp" ng-controller="DemoCtrl" ng-init="half = numbers.length / 2">
    <div>
        <ul>
            <li ng-repeat="number in numbers.slice(0, half)">{{number}}</li>
        </ul>
    </div>
    <div>
        <ul>
            <li ng-repeat="number in numbers.slice(half)">{{number}}</li>
        </ul>
    </div>
</div>

View demo


Method 2 : apply the limitTo built-in filter

<div ng-app="myApp" ng-controller="DemoCtrl" ng-init="len = numbers.length">
    <div>
        <ul>
            <li ng-repeat="number in numbers | limitTo: len / 2 + (len % 2)">{{number}}</li>
        </ul>
    </div>
    <div>
        <ul>
            <li ng-repeat="number in numbers | limitTo: - len / 2">{{number}}</li>
        </ul>
    </div>
</div>

Adding len % 2 helps to handle array oddity.

View demo

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

What is the best way to interpret and execute conditions specified within strings like "condition1 and condition2"?

Received a JSON file from an external source with various conditions that need to be tested, either in real-time or through conversion. Imagine having an instance of a class called Person, with attributes {age: 13, country: "Norway"}, and an ext ...

Displaying various charts in a single view without the need for scrolling in HTML

How can I display the first chart larger and all subsequent charts together in one window without requiring scrolling? This will eventually be viewed on a larger screen where everything will fit perfectly. Any recommendations on how to achieve this? Here ...

The DOM seems to be producing NaN when receiving user input

Currently, I am in the process of developing a depreciation calculator that utilizes user input fields to generate an alert with the resulting value. When I set the variables to predefined test values without incorporating user input, the calculator funct ...

Getting an attribute parameter within a directive: A step-by-step guide

I am currently working on injecting the name from the myController scope into the myObj directive as a custom attribute called nameAttr. Even though I have set name: '=nameAttr' in the directive's scope, it doesn't seem to be functioni ...

Using the div id within JavaScript to create a new Google Maps latitude and longitude

Here is my code for locating an IP using Google Maps. I am trying to set new google.maps.LatLng('< HERE >') to <div id="loc"></div>. How can I achieve this where the result will be 40.4652,-74.2307 as part of #loc? <scrip ...

"Sorry, but the request does not contain multiple parts" - using AngularJS with Spring Boot

This is a question that has been asked many times before, but despite trying all possible solutions, I still encounter the following error: the current request is not a multipart Below is my Angular code snippet: app.controller("controller", function ($ ...

Issue with Generating divs dynamically in Ionic Framework

I'm currently working on dynamically generating a board game using divs in AngularJS, like so: HTML: <div class="boardgame" ng-repeat="square in board"> <div class="square"></div> </div> JS: $scope.board = [ {value: ...

Just starting out with jQuery: seeking advice on a user-friendly slideshow plugin, any tips on troubleshooting?

I am currently trying to incorporate a basic jquery slideshow plugin, but I seem to be encountering some difficulties. The documentation mentions the need to install 'grunt' and 'node dependencies', which is confusing to me as I am new ...

Error encountered during JSON parsing: unexpected non-whitespace character found after the JSON data on line 1

I have implemented a simple JavaScript code (using AngularJS, but that's not important) like this: app = angular.module('app', []); app.controller("NavCtrl",function($scope,$http){ var serviceBase = 'api/'; $http.get(serv ...

Exploring First-Person Shooter Rotation with THREE.JS

I recently attempted to create a rotation system for a First Person Shooter game using THREE.JS. However, I encountered a strange issue where the camera would pause momentarily before returning, especially at certain rotations. When checking the rotation ...

Creating a collection of interconnected strings with various combinations and mixed orders

I am currently working on creating a cognitive experiment for a professor using jsPsych. The experiment involves around 200 logical statements in the format T ∧ F ∨ T with 4 different spacing variations. My main challenge is to figure out a way to a ...

Remove all keys of type BaseType from objects that are children of BaseType

There are two types: BaseType and BabyType. export type BaseType = { id: string } export type BabyType = BaseType & { name: string }; In a generic layer where <T extends BaseType> is used, the item being dealt with is actually a 'B ...

Looking to transfer a file to a server with the help of angularJS, NodeJS, and ExpressJS

I attempted to use the instructions provided in this link but encountered an error The version of Node I am using is 4.4.7, and I received a modulerr error regarding ngFileUpload In order to upload files in app.js var multer = require('mul ...

Is there a way for me to have Next.Js automatically download and serve all the external assets on my website?

Currently, I have been putting together a portfolio using headless WordPress and NextJs. To retrieve data from the GraphQl endpoint, I created my own functions which are working perfectly fine. However, my issue lies with media files such as images and pdf ...

The property 'body' cannot be read because it is undefined

I have been attempting to incorporate passport logic into my controllers file, but I encountered an issue. When I place the logic inside the controllers, it gives me an error message saying "Cannot read property 'body' of undefined." However, whe ...

Using PHP and AJAX, populate a table based on the selection made from a dropdown

Hello, thank you for taking the time to review my issue. Let me outline the objective. I have successfully implemented two drop-down menus that are populated dynamically from a database. The query retrieves names and phone numbers (with plans to fetch mor ...

Display a div when hovering over an image using jQuery

I've spent a good 30 minutes searching on Stack Overflow for a solution, but I haven't been able to find it yet. It's possible that I'm not sure what exactly to look for. Essentially, I have a div that contains an image and some text. ...

What steps can I take to troubleshoot this issue involving Django and AJAX?

Looking to pass the selected ID of an option via an AJAX request to Django 2.1, but encountering errors. As a newcomer to Django and web development, I would appreciate any help in resolving this issue. document.addEventListener('DOMContentLoaded&apos ...

What is the best way to include additional text in my scroll-to-top button that already features an icon?

I'm completely new to this industry, so I could really use some assistance. Despite trying every possible solution, nothing seems to be working for me. Currently, I am looking to add some text to my scroll-to-top button, which already exists with an ...

What could possibly be causing my MongoDB collection to return an empty object?

I've been attempting to retrieve all the data from my "users" collection, but all I keep getting is undefined. Within my directory and file labeled leaderboard/lb.js, and indeed, my database goes by the name of collections: const mongoose = require( ...