Limit the ng-repeat directive based on the length of the array, using an ng-if condition

    <div ng-repeat="badgesData in deptUSersData.badges | limitTo : 7 track by $index">
      ........content.......
    </div>

If the length of deptUSersData.badges is more than 8, I want to apply a limit of 7. How can I achieve this?

My desired solution would look something like this:

<div ng-repeat="badgesData in deptUSersData.badges 
 ng-if="deptUSersData.badges.length > 8" ? limitTo : 7 : '' track by $index>

Answer №1

In the controller, you have the ability to define a variable that can be used to restrict the number of items displayed in an ng-repeat loop, as shown below:

<div ng-repeat="badgesData in deptUSersData.badges | limitTo : myLimit track by $index">
   ........content.......
</div>

app.controller('someController', function($scope) {
    if(deptUSersData.badges.length > 8)
        $scope.myLimit = 7;
  }
);

If the length of deptUsersData.badges is less than or equal to 8, the variable myLimit may end up being undefined.

Referencing the limitTo documentation:

If limit is undefined, the input will be returned unchanged.

This means that if myLimit is undefined, the filter will not be applied and all elements will be displayed.

Answer №2

To get the desired outcome, utilize ng-show by checking the array length and limiting it to 7 using $index < 7

ng-show="{{deptUSersData.badges.length > 8? $index < 7: true}}">

Sample code - https://codepen.io/nagasai/pen/vRRQVw

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.limit = 6;
    $scope.deptUSersData = {
       badges :[
        1,2,3,4,5,6,7,8,9
       ]
    }
  
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>


<div ng-app="myApp" ng-controller="myCtrl">
  <div>
    <div ng-repeat="badgesData in deptUSersData.badges track by $index" ng-show="{{deptUSersData.badges.length > 8? $index < 7: true}}">
      {{badgesData}}
    </div>
  </div>
</div>

Note: Adjust the array length to 8 or lower for a different display

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 store checkbox statuses in local storage and display them again in a JavaScript to-do list?

I'm currently working on a to-do list application using basic JavaScript. One issue I'm facing is saving the checked status of the checkbox input element and displaying it again after the page is refreshed. Since I'm still learning JavaScrip ...

Use Jquery to apply quotation marks within a blockquote

Here is the code I am working with: $("input[id="+id.slice(0,-1)+"-br-"+brand+"].qnt_to_cart").show(); This code generates: input[id=02620-br-FEBI BILSTEIN].qnt_to_cart However, I need it to look like this instead: input[id="02620-br-FEBI BILSTEIN"].q ...

Is it possible for me to hand over control to a child process and retrieve the result in Node.js?

Recently, I encountered an issue where multiple simultaneous GET requests to my Node.js server caused it to become overwhelmed and unresponsive, leading to timeouts for clients (503, service unavailable). Upon thorough performance analysis, I discovered t ...

AngularJS filtering with multiple conditions

My ng-repeat list is currently displaying a collection of objects with a filter applied to only show objects inserted today. Here is how it looks: <div class="row msf-row" ng-repeat="record in recordlist | filter: record.date = dateJson"> Whi ...

Distinguishing between selecting rows and selecting checkboxes in Mui Data Grid

Is there a way to differentiate between clicking on a row and clicking on the checkboxes? I want to be able to select multiple rows by clicking anywhere in the row except for the checkbox column. When clicking outside the checkbox column, I would like to p ...

My program is throwing an error due to invalid JSON format

Snippet of code: var data = $.parseJSON(data); Error message: Encountered Invalid JSON: {times: [{'9:30am','10:00am','10:30am','11:00am','11:30am','12:00pm','12:30pm','1:00pm&apo ...

Creating dynamic charts in Javascript using Firebase

My dad recently asked me to enhance our motor home by integrating an Arduino with Firebase to monitor the water and propane tanks. He's hoping to be able to check tank levels from his phone so he can see when they need refilling. I've successful ...

Update a specific element within Angular framework

Just starting out with angular and facing a seemingly simple issue that I can't seem to solve despite trying various solutions found on SO. I have created a login component where upon submission, the user is redirected to their profile page. While I a ...

The Angular Date Pipe is currently unable to display solely the Month and Year; it instead presents the complete date, including day, month,

I'm currently using the Bootstrap input date feature to save a new Date from a dialog box. However, I only want to display the month and year when showing the date. Even though I added a pipe to show just the month and year, it still displays the mont ...

Preflight request rejection due to access control check failure in React and Express

I am facing a similar issue to this question, but my problem is specific to using Express with React on the frontend. The error I am encountering is: https://i.sstatic.net/1OIfy.png Access to fetch at 'http://localhost:8000/api/users/auth/github& ...

What improvements does IE7 offer compared to IE6?

Many developers in the web development industry often express frustration when it comes to developing for IE6. But when working with a powerful JavaScript framework such as jQuery, does the process of developing for IE6 differ significantly from that of ...

What is the best way to incorporate keyboard shortcuts into carousel operations using jQuery?

The example can be found here. I want to be able to navigate through the images using the left and right arrows on my keyboard. How can I achieve this using jQuery or CSS? This is the structure of the HTML: <div id="slider-code"> <a cla ...

Are you in need of a BISON middleware solution for socket.io encoding and decoding?

Is there a method to manipulate the data transmitted by socket.io just before it is sent or received? I was considering implementing something like an express middleware. This way, I could encode the data after the normal .emit() call and before the socket ...

Tips for preserving images while browsing a website built with Angular single-page application

Utilizing Angular's router for component navigation has been beneficial, but I am facing an issue with component reloads when going back. To address the problem of content reloading from the server, I have implemented a solution where the content arra ...

Why aren't Material UI v5 styles being applied to the class?

I have been attempting to customize the MUI slider by applying styles using the className prop. However, I am facing an issue where the styles assigned to the main class are not being applied, but other styles such as the 'hover' state are workin ...

The issue with the Sails.js application is that it fails to update files from assets after

Currently, I am working on a Sails.JS application with Angular.JS as its front-end framework. All the angular files are stored in /assets/linker and they are correctly injected upon start. However, I have encountered an issue where any changes made to the ...

Struggling with determining the perfect transition speed for the sidemenu display

I'm a newcomer to web development and facing an issue with setting the transition speed for opening and closing this side menu. Despite adding transitions in the CSS and specifying duration in the Javascript, the menu continues to open instantly. I di ...

Prevent drag and drop functionality in QtWebkit

As I work on creating an HTML GUI for my application using Qt's WebKit, everything is going smoothly with one minor issue. I am trying to prevent users from dragging and dropping images from the GUI itself. While I have successfully disabled text sele ...

Changing the shading of an arc in d3.js: Tips and tricks

I've been attempting to create a gauge with a needle that changes the background color of the ring for the past few days. My goal is to dynamically update the colors of an arc within a gauge I developed in d3.js This is my current gauge setup with t ...

Is it an error to pass arguments using square brackets when requiring a node module?

Recently, I came across this piece of nodeJS code on a GitHub repository: var env = process.env.NODE_ENV || 'development' , config = require('./config/config')[env] , auth = require('./config/middlewares/authorization') , mon ...