Including Previous and Next buttons in an AngularJS ngRepeat array for navigating through multiple items

Looking to create a dynamic slider showcasing multiple players using ng-repeat in Angular 1.6. The goal is to have prev/next buttons within the ul>lis to navigate through the array of players and view them one by one.

HTML----sliderdemo.html

<div ng-controller="SliderDemoCtrl">
   <div class="champions-slider">
            <ul class="team-members list-inline text-center" style="display:flex" >
                <li ng-repeat="player in players | limitTo: 4" style="padding:10px"> 
                    <div class="img-holder">
                        <a href="/player/{{ player.name }}"><img ng-src="{{ player.image }}" alt="{{player.name}}" width="20px"></a>
                    </div>
                    <strong class="name">{{ player.name }}</strong>
                </li>
            </ul>
            <a href="#" class="btn-prev" ng-click="?????">Prev</a>
            <a href="#" class="btn-next"ng-click="?????">Next</a>
    </div>
</div>

JS controller---slider.demo.controller.js

 var myApp = angular.module('slider.demo', []);
 myApp.controller('SliderDemoCtrl',['$scope',function($scope){
 $scope.players = [
   {
    image:"http://placehold.it/500/e499e4/fff&amp;text=1",
    name: "tes 1"
   },
   {
    image:"http://placehold.it/500/e499e4/fff&amp;text=2",
    name: "tes 2"
   },
   {
    image: "http://placehold.it/500/e499e4/fff&amp;text=3",
    name: "tes 3"
   },
   {
    image:"http://placehold.it/500/e499e4/fff&amp;text=4",
    name: "tes 4"
   },
   {
    image:"http://placehold.it/500/e499e4/fff&amp;text=5",
    name: "tes 5"
   },
   {
    image: "http://placehold.it/500/e499e4/fff&amp;text=3",
    name: "tes 6"
   }
  ];
}]);

Take a look at the plunkr for more details: https://plnkr.co/edit/J7dxeMfM22ju5gpZl5ri?p=preview

Any assistance will be highly appreciated.

Thank you!

Answer №1

It seems that you are looking for a solution similar to the following:

// Here is the code snippet

var myApp = angular.module('slider.demo', []);
myApp.controller('SliderDemoCtrl',['$scope',function($scope){
    $scope.players = [
      {
        image:"http://placehold.it/500/e499e4/fff&amp;text=1",
        name: "tes 1"
      },
      {
        image:"http://placehold.it/500/e499e4/fff&amp;text=2",
        name: "tes 2"
      },
      {
        image: "http://placehold.it/500/e499e4/fff&amp;text=3",
        name: "tes 3"
      },
      {
        image:"http://placehold.it/500/e499e4/fff&amp;text=4",
        name: "tes 4"
      },
      {
        image:"http://placehold.it/500/e499e4/fff&amp;text=5",
        name: "tes 5"
      },
      {
        image: "http://placehold.it/500/e499e4/fff&amp;text=3",
        name: "tes 6"
      },
    ];
    $scope.size=0;
    $scope.next=function(){
      if($scope.size+4==$scope.players.length)
        return;
       $scope.size+=1;
      
    }
    $scope.prev=function(){
      if($scope.size==0)
       $scope.size=0;
      else 
       $scope.size-=1;
      
    }
}]);
<!doctype html>
<html ng-app="slider.demo">
  <head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js"></script>
    <script src="script.js"></script>
  </head>
  <body>

<div ng-controller="SliderDemoCtrl">
  <div class="champions-slider">
<ul class="team-members list-inline text-center" style="display:flex" >
<li ng-repeat="player in players | limitTo: 4" style="padding:10px"> 
<div class="img-holder">
<a href="/player/{{ players[$index+size].name }}"><img ng-src="{{ players[$index+val].image }}" alt="{{players[$index+val].name}}" width="20px"></a>
</div>
<strong class="name">{{ players[$index+size].name }}</strong>
</li>
</ul>
<a href="#" class="btn-prev" ng-click="prev()">Prev</a>
<a href="#" class="btn-next"ng-click="next()">Next</a>
</div>
</div>
  </body>
</html>

Answer №2

To implement pagination in your Angular app, utilize ng-if with the help of $index. Check out this Plunker

    $scope.pre = 0;
    $scope.nex = 4;

    $scope.nextItem  = function(){
      $scope.pre = $scope.nex;
      $scope.nex = $scope.nex + 4;
    }

    $scope.prevItem  = function(){
      $scope.nex = $scope.pre
      $scope.pre = $scope.pre - 4;
    }

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

Using JavaScript to create a paging system for a gallery of images with a maximum number

Let me explain my current setup: I have an image gallery on my website displaying 10 images at a time along with a pagination bar. These images are populated using a loop that iterates over a JSON file. It's all working smoothly so far! The code look ...

Why does the <select> input field, populated with $.each, only get filled once when dynamically creating input fields?

I am facing an issue while trying to dynamically add an input field to a form. Everything seems to work fine except for one thing: When I try to populate a select dropdown with options using $.each, it only works for the first dynamically added field. If ...

Is it the case in JavaScript that after executing `const newArray = oldArray.splice(0)`, the resulting array `newArray` includes any

When looking at my JavaScript code, I have come across a particular line that stands out: const newArray = oldArray.splice(0); This line raises questions about what would happen if there are concurrent modifications to oldArray while this line is being e ...

UPDATE: An issue has been identified with the /login route that is currently causing an unknown error

Hours of coding and suddenly my app is rendering everything twice. Can't figure out why, please help! https://i.sstatic.net/RhMid.png app.js import { BrowserRouter as Router, Switch, Route } from "react-router-dom"; import "bootstrap/ ...

Guide on adjusting PHP variable values and updating functions with ajax requests

One of my functions determines dates based on a variable For example: $modification = "+1 Week" function updateCalendar($change){ $month = array("January","February","March","April","May","June","July","August","September","October","November","Dece ...

I am interested in utilizing the image.onload method within the $.when function

I am trying to dynamically load images inside a loop with specific requirements. In the code below, I want the alert to fire only when an image is being loaded, and then another alert to fire only after the first alert has fired. Here is my code: for (i ...

What is the best way to retrieve the src value upon clicking on an image in a JavaScript function with multiple images displayed on

I have 3 images on my website. Each time I click on an image, I want to retrieve the source value. I attempted the code below, but it doesn't seem to work with multiple images. <div class="test"> <a href="#" class="part-one" ...

Utilizing Ionic to implement a conditional statement for comparing a string with information retrieved from an Observable source

I have a piece of code where I fetch data about a country as an observable. I then attempt to compare my string this.city with the this.capital that I got from the Observable. If they are not the same, I want to show a new paragraph in the HTML by changi ...

Determine the sum of all the cumulative cart item totals using Angular

Here is a basic shopping cart setup with individual controllers for each item in the cart: <!DOCTYPE HTML> <html ng-app="cart"> <div ng-controller="cartCtrl"> <table> <tr><th>qty</th><th>prize< ...

angularFire encounters difficulties when attempting to add information to Firebase

I am currently utilizing angularFire and attempting to save form data to firebase using $add. Any assistance on this matter would be highly appreciated. All the console logs are showing correctly, as I am able to view the data in the console. Apologies for ...

Exploring Strategies for Optimizing HTTP Error Messages

My current challenge involves sending a JSON-encoded data string to a remote machine via AJAX. Every time I attempt to transmit the data string, one of two error messages emerges: XMLHttpRequest cannot load http://10.1.0.139:8000/. No 'Access-Control ...

The FlatList component with a specified number of columns is failing to display the desired

Hey guys, I have a food list data stored in JSON format as shown below: [ { "id": 0, "key": " Viande hachee", "checked": false, "image": "https://i.imgur.com/RuVD8qi.png " }, { "id": 1, "key": " Escalope pane", "check ...

How can I prevent vuejs watch methods from being triggered when the value is modified by the same method?

monitorChanges: { observeValue() { setTimeout(() => { this.observeValue = ""; }, 4000); } } In this scenario, the observeValue function is initially invoked by the DOM, but it repeats when the value ...

Converting numerical elements in an array to strings

Looking for assistance with reformatting the values in this Array : [{abcd:1, cdef:7},{abcd:2, cdef:8}, {abcd:3, cdef:9}] I want to convert all the values into strings like so : [{abcd:"1", cdef:"7"},{abcd:"2", cdef:"8"}, {abcd:"3", cdef:"9"}] Can anyo ...

What is the best way to display the remaining items in an array when a user clicks the next button?

Having a total of 12 cameras, I want to be able to select 4 cameras from a drop-down menu option and have only those 4 cameras displayed. Then, when I click on the next button, I need to show the remaining cameras in the selected layout format. How can thi ...

What is the best way to access the onclick calling object?

Is there a way to have a handler on the calling object of the onclick event? <a href="123.com" onclick="click123(event);">link</a> <script> function click123(event) { //I need access to <a> in order to man ...

Unable to utilize JavaScript objects extracted from JSON

Working on developing a web application using AngularJS has led me to encounter an issue. I have a PHP server that retrieves data from an SQL database and encodes it into JSON. Utilizing the Angular $http service on the client side, I am able to successful ...

What methods can be used to automatically direct users to a specific page in my app once they have completed the registration process?

It's possible that I'm not placing the second router.replace('/contact'); line correctly. However, it seems strange because the redirect works fine when a user logs in. Any help would be greatly appreciated. if (isLogin) { const r ...

Issue encountered while creating a token in a JavaScript Chrome extension and attempting to validate it on a backend Node.js server

Trying to create a token within a chrome extension and utilizing it to authenticate requests to the backend server has proven challenging. While successfully generating a token on the front end, encountering an error when verifying it with the google-auth- ...

Experiencing incorrect outcome when using the Number.isInteger() function in JavaScript

I have been experimenting with the Number.isInteger() method on the Chrome console. After running a for loop and checking the result using console.log(arr);, I noticed that the array only contains a single value of 1, like this: [1]; var arr = [1, 2, 3, ...