Styling the first visible item in ngRepeat using Angular

I am working with a list that is generated using ngRepeat, which looks like this

<ul class="list-group">
    <li class="list-group-item" ng-repeat="data in tree | filter:key">
        {{data.name}}
    </li>
</ul>

My goal is to make the first item in the list have rounded corners. I have defined a style as shown below

.first-item {
  border-top-left-radius: 15px;
  border-top-right-radius: 15px;
}

How can I apply this style to only the first visible item? Keep in mind that the visibility of the first item may change dynamically based on the search input connected to the key.

Answer №1

utilize the $first variable to represent the initial iteration in an ng-repeat loop

<ul class="list-group">
    <li ng-class="{'initial-item':$first}" class="list-group-item" ng-repeat="data in tree | filter:key">
        {{data.name}}
    </li>
</ul>      

update: because initial-item includes a hyphen, it needs to be enclosed in quotation marks

Answer №2

To apply rounded borders to the first item in a list group, you can utilize the css selector first-child:

.list-group li:first-child { 
     border-top-left-radius: 12px;
     border-top-right-radius: 12px;
}

Answer №3

The most effective method in Angular is:

<ul class="list-group">
    <li class="list-group-item" ng-repeat="item in tree | filter:search track by $index" ng-class="{Highlighted: $index == 0}">
        {{item.name}}
    </li>
</ul>

Alternatively, you can refer to Sasi Kiran's solution for more insights ;)

Answer №4

To implement conditional formatting based on the $index variable in an ng-repeat loop, you can follow this example:

<ul class="items-list">
    <li class="item" 
        ng-repeat="item in items | filter:searchTerm" 
        ng-class="{'first-item': $index == 0}">
        {{item.name}}
    </li>
</ul>

Answer №5

To achieve this effect, you can utilize the $first and ng-class directives. The $first property is accessible on the local scope for each item in the repeated list. It evaluates to true if the item is the first one in the list. You can implement this in your HTML code like so:

<ul class="list-group">
<li class="list-group-item" ng-repeat="data in tree | filter:key" ng-class="{'.first-item': $first}">
    <div>{{data.name}}</div>
</li>

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 value of the bound variable in ng-options does not get updated after the array is

I have a situation where I am populating a list using an array and the ng-options directive in AngularJS. The selected item is bound to a variable called myObject.selectedItem. However, I noticed that even after clearing the array, the value of myObject.se ...

Enhancing real-time updates with discrete solutions

Currently, I am developing an ASP.NET Core application using Angular as the front end. Let's consider a specific scenario: when a client sends a request, the server has to generate numerous mathematical models and calculate them on certain infrastruct ...

The model name should not be overridden if it is already being used in different fields

I have a model that I am binding on two sides. The first side is inside an html p tag, and the second side is a textarea. When I make changes to the textarea, the content inside the p tag also changes. How can I ensure that only the content in the textar ...

Why isn't my topmenu positioned correctly on screens of varying widths due to responsive CSS?

Welcome, I am in the process of learning CSS and responsive design. I have an index page that consists of a header, top menu, main text content, and footer. The top menu layout works well up to 300px, but when it goes under 300px, only the additional link ...

After deploying DRF, the CSS in Django admin is not displaying

After developing a web application using Django Rest Framework and React, I faced an issue during deployment on IIS. While the deployment is successful, I encountered a problem with the Django Admin where the styles were not displaying properly. This led t ...

Minimum width of Angular Material's mat-menu

I am looking to create a compact Material mat-menu using Angular 15. Below is the code I have: <mat-menu #flagMenu="matMenu"> <button mat-menu-item> <img src="assets/flags/en.png" class="flag"/> ...

Achieving Full Height for Parallel Columns Divs with Bootstrap - A How-To Guide

I am struggling with getting two divs in parallel to have full height. Even though I have the divs side by side, when I try to set their height:100%;, nothing happens. What CSS properties do I need to modify to achieve this? The goal is to create a cove ...

jQuery-UI tabs appearing on various elements

My web page layout includes three different divs: header, wrapper, and footer. The header is set to a fixed position. Within the wrapper, there are various components, including some jqueryUI tabs. However, as I scroll down the page, the wrapper and its ...

Styling a PrimeFaces panelGrid within a data table using CSS

I am struggling to add a background color to a panelgrid within a datatable when a hover event occurs. Despite writing the necessary code and CSS, nothing seems to work. Any suggestions on how to address this issue? <p:dataTable id="movementsTable" var ...

Having trouble getting the ionic lib update command line to work properly?

My current situation: $ionic lib update you sure you want to replace D:\Projects\cda-volunteer\www\lib\ionic with an updated version of Ionic? (yes/no): yes Unable to receive version data Here's my ionic info: Cordova ...

Guide on linking AngularJS user interface with Python server via RESTful API?

Can someone please explain the process of connecting two parts? Specifically, I am using Angular CLI for the front-end and Python for the logic. My goal is to send text responses from the front-end to my Python back-end and receive a response in return. ...

.htaccess rules for rewriting URLs in AngularJS using html5mode and ui-router

Although this question has been asked numerous times on Stack Overflow, I am still unable to achieve the desired results. I have attempted to use this link: htaccess redirect for Angular routes You can find more information at: https://github.com/angular ...

Once an element is removed from ng-repeat, it does not provide the capability to remove or conceal the element from

The following code is used to display records using ng-repeat: <div class="gallery"> <div ng-cloak ng-repeat="photo in photos| orderBy:'-id'" ng-mouseLeave = "delete_btn = !delete_btn" ng-m ...

Angular - Dynamically Apply Attributes Based on Screen Size

I have tried several solutions to no avail. In the following div, I am using the attribute equalizer: <div class="text-center" equalizer='group'> However, I only want the equalizer attribute to be present if the window width is greater th ...

AngularJS fails to display JSON data

I've been experimenting with AngularJS by creating a basic gallery that pulls images from a JSON file, but for some reason I'm having trouble displaying the data. index.html: <!DOCTYPE html> <html ng-app="portfolioApp"> <head> ...

Table borders not displaying properly in Chrome when cells are hidden

I'm dealing with a peculiar issue regarding the display of borders in Chrome when individual cells are hidden within an HTML table. The borders disappear, despite being defined for the row and table elements. Does anyone have experience with this spec ...

Text affected by mix-blend-mode glitch

Currently, I am experimenting with knockout text using the mix-blend-mode property. An interesting issue arises when I examine the responsiveness of my design by utilizing Developer tools in the browser console - there are faint lines that momentarily appe ...

How about using a circle inset clip path technique?

Can a unique inset circle clip path be created to cut a hole through the center of a div instead of just showing the center? The entire div should be visible except for a hole cut out in the center to achieve a design similar to this: https://i.sstatic.n ...

Applying Compiled CSS file in Node.js using Express and SASS not working as expected

Recently, I've delved into utilizing NodeJS with Express to serve my pug files. In addition, I decided to incorporate the "express-sass-middleware" to compile and serve the scss/css files in my project. While everything seems to be functioning as expe ...

I'm having trouble getting my button to float correctly on the right side

Here I am facing a unique situation where I am attempting to align my button input[type="submit"] to the right side. I have allocated space for it and therefore trying to float the button to the right so that it can be positioned next to my input text. U ...