What is the best way to display a specific number of list items using AngularJS?

My goal is to display 3 li elements initially, and after a delay of 1 second, these 3 li items will slide up while the next set of 3 li elements automatically appear in the div.#content.

<div id="content">
    <ul>
        <li>122</li>
        <li>first</li>
        <li>second</li>
        <li>third</li>
        <li>fourth</li>
        <li>fifth</li>
        <li>sixth</li>
    </ul>
</div>

I was able to achieve this using jquery, but I am now looking to implement it with angularJs. However, I am not very experienced with angularjs. If anyone could offer some guidance, I would greatly appreciate it. You can view the current implementation using jquery here.

Answer №1

If you're only dealing with translation, then execute the code snippet

(function ()
{
    var app = angular.module("app", []);

    function HomeController($interval)
    {
        var vm = this;        
        var i = 2,
            $ul = angular.element('#content ul');
        int = $interval(function ()
            {
                angular.element('li', $ul).slideUp();
                angular.element('li' + (i == -1 ? '' : ':gt(' + i + ')') + ':lt(3)', $ul).slideDown();
                i += 3;
                if (i + 1 >= angular.element('li', $ul).length) i = -1;
            },
            2000);
    }

    app.controller("HomeController", ["$interval",HomeController]);

})();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html lang="en" ng-app="app">
    <head>
        <meta charset="UTF-8">
        <title>Angular JS App</title>        
    </head>
    <body>

        <div class="container" ng-controller="HomeController as homeCtrl">


            <div id="content">
                <ul>
                    <li>1</li>
                    <li>2</li>
                    <li>3</li>
                    <li>4</li>
                    <li>5</li>
                    <li>6</li>
                    <li>7</li>
                    <li>8</li>
                    <li>9</li>
                </ul>
            </div>           

        </div>
    </body>
</html>

Answer №2

Take a look at this Plunker. I have eliminated jQuery and implemented some mathematical logic instead.

let index = 0;

    const mainList=[1,2,3,4,5,6,7,8,9];

    $scope.list = [mainList[index], mainList[index+1], mainList[index+2]];

    $ul = angular.element('#content ul');

    interval = $interval(function ()
        {
             if(index > 8){
              index = 0;
            }
            $scope.list = [mainList[index], mainList[index+1], mainList[index+2]];
        },
        2000);

Answer №3

Give this a shot:

angular.module('app', [])
    .directive('slideRoller',function($interval){
        return {
            restrict:'A',
            link:function(scope,ele,attrs){
                var interval = attrs.interval || 2000;
                var children = ele.find('li');

                var start = -1;
                roll(start);
                $interval(function(){
                    start += 3;
                    if(start>=children.length-3){
                        start = -1;
                    }
                    roll(start);
                },interval);

                function roll(i){
                    angular.forEach(children,function(val,index){
                        if(index>i && index<=i+3){
                            angular.element(val).removeClass('slide-up').addClass('slide-down');
                        }else{
                            angular.element(val).removeClass('slide-down').addClass('slide-up');
                        }
                    });
                }
            }
        }
    })
;
.slide-up{
max-height: 0;
overflow-y: hidden;
}

.slide-down {
max-height: 10em;
-webkit-transition: max-height 0.5s ease-in-out;
-moz-transition: max-height 0.5s ease-in-out;
-o-transition: max-height 0.5s ease-in-out;
transition: max-height 0.5s ease-in-out;
}
<html>
<head>
    <title>Example Page</title>
    <script src="//cdn.bootcss.com/angular.js/1.4.7/angular.js"></script>

</head>

<body ng-app="app">
    <div class="container">
        <ul slide-roller interval="1000">
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
            <li>6</li>
            <li>7</li>
            <li>8</li>
            <li>9</li>
        </ul>
    </div>
</body>
</html>

Answer №4

Here's some helpful information for you:

If you decide to import jQuery, AngularJs will incorporate it, making it acceptable to use jQuery.

However, if you choose not to import jQuery, AngularJs will utilize jQlite instead, which is very similar to jQuery.

According to the official documentation:

Does Angular depend on the jQuery library? Yes, Angular can make use of jQuery if it exists in your app at bootstrap time. If jQuery is unavailable, Angular will default to its own implementation known as jQLite that mirrors a subset of jQuery.

It is important to note that Angular 1.3 only supports jQuery version 2.1 or higher. While jQuery 1.7 and newer may function correctly with Angular, it is not guaranteed.

For more details, visit the FAQ page: https://docs.angularjs.org/misc/faq

Learn about Angular jqLite here: https://docs.angularjs.org/api/ng/function/angular.element

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

The preventDefault() function is not functioning properly on the <a> tag

As a JavaScript beginner, I decided to create an accordion menu using JavaScript. Although I was successful in implementing it, I encountered a bug in my program. In this scenario, uppercase letters represent first-level menus while lowercase letters repr ...

Step-by-step guide to triggering the inactive dropdown function by selecting a different menu item

This issue seems to have cropped up recently, possibly due to multiple changes being made. The main problem is figuring out how to deactivate a dropdown menu when another menu item is clicked. As shown in the image below, it appears that two items are sele ...

Issues with sending emails through Nodemailer in a Next.js project using Typescript

I'm currently working on a personal project using Nodemailer along with Next.js and Typescript. This is my first time incorporating Nodemailer into my project, and I've encountered some issues while trying to make it work. I've been followin ...

Ways to retrieve a value from a span using the Document Object Model

sample.html <span id='char'>{{value}}</span> sample.ts console.log((<HTMLInputElement>document.getElementById('char'))) This code snippet will display <span id='char'>ThisIsTheValueupdated</sp ...

What is the origin of the libraries found in npm-shrinkwrap that do not match the packages listed in package.json?

I'm puzzled by the presence of `express` in my `npm-shrinkwrap` file as a main dependency. Despite this, `express` is not listed as a dependency in my `package.json` file. I can't find any usage of it in my project. It's not included a ...

Instructions for making a crossword-inspired grid using a high quantity of divs or alternative elements

I am looking to create a grid on the screen of a web browser or mobile device, with each grid item containing just one letter. It's similar to a crossword puzzle, but taking up most of the screen. I've tried using divs for this purpose, but it se ...

Tips on how to conditionally render a button based on the presence of a link from a separate file in React

I've been working on my personal portfolio site using React and Tailwind. One challenge I'm facing is how to display the "GitHub" button for each project card only when a GitHub link is provided in my data.js file. Currently, I'm utilizing ...

What are some ways to prevent sorting and dragging of an element within ul lists?

A list styled with bullets contains various items. It is important that the last item in the list remains in a fixed position. I attempted to disable sorting using the cancel option of the .sortable() method, but it only prevented dragging without disablin ...

Is there a way to include a Spring Security JSESSIONID while utilizing SockJS and STOMP for cross-domain requests?

Currently, I am facing an issue with my setup which involves 3 different use cases - two of them are functioning properly while the third one is not. The configuration includes an AngularJS client using SockJS with STOMP. The backend is a Spring applicati ...

Displaying different content inside a div based on the selection made in a dropdown menu

I'm experiencing difficulties with a jQuery script that should display the content of a div based on selected options in a drop-down menu. The drop-down menu is supposed to provide numerical values for a calculator to determine a loan amount. However ...

Selecting multiple images by holding Ctrl key and uploading them through PHP

Looking for guidance on creating a Facebook-like multiple image uploader in my PHP project. The user should be able to select multiple files with Ctrl+click. Anyone know of a suitable jQuery plugin or have some sample code handy? Appreciate any help. Than ...

Blank white screen in Three.js following the rendering of numerous objects

I have a situation where I am loading over 4350 3D objects from JSON files in my game. for (var y in game.fields) { for (var x in game.fields[y]) { switch (game.fields[y][x]) { case 'm': ...

Retrieving two values from a 2-dimensional array but displaying only one

Hey there, I'm trying to extract the first word from an array I've created and then display its antonym upon clicking a "flip" button. Basically, I want to fetch the 0th and 1st elements but only reveal the 0th one initially until the user clicks ...

Only the initial TinyMCE editor that was added dynamically is visible, the rest are not showing

I am currently developing a javascript application that will showcase a real-time discussion thread. The app will regularly communicate with the server to fetch new data and display it on the page. To enable users to post comments and replies, we have deci ...

I am attempting to add a button at the conclusion of every row within my table using JavaScript

My challenge is to add buttons to the end of each row in a dynamic table using JavaScript for appending. However, every time I attempt this, it only adds a button at the end of the last row. Here's an image depicting the issue: View Image <!--TABLE ...

On smaller screens, the top placement of right sidebar links is necessary

Our website layout is organized into 3 main sections: the top part contains the main menu, followed by the main content and a sidebar specific to each page. The main content and sidebar are structured using bootstrap col-* classes. On small screens, I en ...

React and React Router are causing the login screen layout to persistently display

The MUI Theme Provider I have includes a Layout with Dashboard and Order screens. Even though the user hits the '/' endpoint, the Login Screen should be displayed instead of the Layout. -App.js <ThemeProvider theme={theme}> <Router> ...

The webpage button isn't properly redirecting, even after attempting to use AJAX. I'm having trouble grasping the concept behind it

Can someone help me modify this code so that when the button is clicked, it redirects to "vote.php" and passes the selected list values and checkbox values through the URL? <?php require_once('Connections/was.php'); ?> <head> <sc ...

Adjust the color of radio button text with Jquery

Here is a sample of radio buttons: <input type='radio' name='ans' value='0'> Apple <input type='radio' name='ans' value='1'> Banana <input type='radio' name='ans&apo ...

Checking the types of arrays does not function properly within nested objects

let example: number[] = [1, 2, 3, 'a'] // this code block correctly fails due to an incorrect value type let example2 = { demo: 1, items: <number[]> ['a', 'b'], // this code block also correctly fails because of ...