"Struggling with AngularJS nth-child not functioning properly within an ng-repeat loop

I've been working on changing the styling of every second row of content displayed on the page using ng-repeat. I managed to make it work for font color, however, I'm struggling with changing the background color.

Below is my code. I would appreciate any suggestions on what I might be doing wrong.

<div class="row" style="margin: 25px 0;">
<div ng-repeat="katt in minaKatter" class="cat-row-ads test" ng-if="!katt.isDeceased && katt.canBeBreedingMale"> 
    <div class="tableContainer test col-xs-12">
        <div class="col-xs-12 col-md-6">
            <img src="data:image/jpeg,{{katt.imageId}}" alt="" class="row-image" style="margin-right: 10px; max-width: 40px;">
            <span style="font-weight: bold; font-size: 12px; line-height: 30px;" ng-bind="katt.name"></span>
        </div>

        <div class="col-xs-12 col-md-3" style="line-height: 30px;">
            <div ng-if="katt.avelshane && katt.status == 1">
                <span class="label-style label label-default">ANNONSERA</span>
            </div>
            <div ng-if="katt.annonsTyp == '1'">
                <span class="label-style label label-default">TILLHÖR</span>
            </div>
            <div ng-if="katt.annonsTyp == '3' && katt.status == 1">
                <span class="label-style label label-default">ANNONSERA</span>
            </div>
        </div>

        <div class="col-xs-12 col-md-3">
            <a href="create-male-ad.php?catId={{katt.id}}" ng-if="(katt|| katt) && (katt== 2 || !katt)" class="btn btn-back btn-sm"><i class="fa fa-pencil" aria-hidden="true"></i> Skapa / Redigera annons</a>
        </div>
    </div>

</div></div>

CSS:
.tableContainer {
    padding: 5px 8px; 
}

.test:nth-child(odd){
    color: red;
    background-color: black;
    border-bottom: 1px solid orange;
}

Answer №1

There seems to be an issue with the nth-child class that has been applied incorrectly. It checks for the nth-child of the corresponding class name. To resolve this, the HTML code should be modified as follows:

<div class="row" style="margin: 25px 0;">
<div ng-repeat="katt in minaKatter" class="cat-row-ads test" ng-if="!katt.isDeceased && katt.canBeBreedingMale"> 
    <div class="tableContainer col-xs-12">
        <div class="col-xs-12 col-md-6">
            <img src="data:image/jpeg,{{katt.imageId}}" alt="" class="row-image" style="margin-right: 10px; max-width: 40px;">
            <span style="font-weight: bold; font-size: 12px; line-height: 30px;" ng-bind="katt.name"></span>
        </div>

        <div class="col-xs-12 col-md-3" style="line-height: 30px;">
            <div ng-if="katt.avelshane && katt.status == 1">
                <span class="label-style label label-default">ANNONSERA</span>
            </div>
            <div ng-if="katt.annonsTyp == '1'">
                <span class="label-style label label-default">TILLHÖR</span>
            </div>
            <div ng-if="katt.annonsTyp == '3' && katt.status == 1">
                <span class="label-style label label-default">ANNONSERA</span>
            </div>
        </div>

        <div class="col-xs-12 col-md-3">
            <a href="create-male-ad.php?catId={{katt.id}}" ng-if="(katt|| katt) && (katt== 2 || !katt)" class="btn btn-back btn-sm"><i class="fa fa-pencil" aria-hidden="true"></i> Skapa / Redigera annons</a>
        </div>
    </div>

</div></div>

The element with the test class inside the element with the tableContainer class is unnecessary.

Please see the revised snippet below.

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.minaKatter = [{imageId: 0}, {imageId: 1}, {imageId: 2}, {imageId: 3}]
});
.tableContainer {
    padding: 5px 8px; 
}

.test:nth-child(odd){
    color: red;
    background-color: black;
    border-bottom: 1px solid orange;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<div ng-repeat="katt in minaKatter" class="cat-row-ads test"> 
    <div class="tableContainer col-xs-12">
    Test
    </div>
</div>

Answer №2

To enhance the styling of your elements, I recommend utilizing distinct classes and implementing a rule for alternating indexes within your ng-repeat using the ng-class feature:

<div class="row" style="margin: 25px 0;">
    <div ng-repeat="katt in minaKatter" class="cat-row-ads test" ng-if="!katt.isDeceased && katt.canBeBreedingMale"> 
        <div ng-class="{1:'tableContainer test-red col-xs-12', 0:'tableContainer col-xs-12'}[$index % 2 == 0]" class="tableContainer test col-xs-12">
            <div class="col-xs-12 col-md-6">
                <img src="data:image/jpeg,{{katt.imageId}}" alt="" class="row-image" style="margin-right: 10px; max-width: 40px;">
                <span style="font-weight: bold; font-size: 12px; line-height: 30px;" ng-bind="katt.name"></span>
            </div>

        <div class="col-xs-12 col-md-3" style="line-height: 30px;">
            <div ng-if="katt.avelshane && katt.status == 1">
                <span class="label-style label label-default">ANNONSERA</span>
            </div>
            <div ng-if="katt.annonsTyp == '1'">
                <span class="label-style label label-default">TILLHÖR</span>
            </div>
            <div ng-if="katt.annonsTyp == '3' && katt.status == 1">
                <span class="label-style label label-default">ANNONSERA</span>
            </div>
        </div>

        <div class="col-xs-12 col-md-3">
            <a href="create-male-ad.php?catId={{katt.id}}" ng-if="(katt|| katt) && (katt== 2 || !katt)" class="btn btn-back btn-sm"><i class="fa fa-pencil" aria-hidden="true"></i> Skapa / Redigera annons</a>
        </div>
    </div>
</div></div>

CSS:

.tableContainer {
    padding: 5px 8px; 
}

.test-red{
    color: red;
    background-color: black;
    border-bottom: 1px solid orange;
}

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

Guide for adding a rotating fontawesome icon within a hyperlink?

I am currently utilizing FontAwesome 3.1.0, and I have encountered an issue with a simple code snippet that is not functioning as expected: <a href="/"> <!-- other html content --> <i class="icon-spinner icon-spin"></i> < ...

Configuration of Bootstrap "col" without an xl number

Hello everyone, I am currently working on a project using Bootstrap, where I have a row with five child elements: <div class="container"> <div class="row"> <div class="col">[content]</div> ...

Conceal a component when the succeeding one appears on a separate line

I'm looking for a solution to display an address on a webpage in two different formats based on the length. The first format should be: Street Number, PostalCode City If the address is too long to fit on one line, it should be displayed as: Street ...

How to align a child element in the center when the parent is not centered or taking up the full width

I am currently working on building a layout with Bootstrap 4.6, and I am facing an issue with centering the header, columns/cards, and buttons on the page width while the parent container is left-aligned and not full width. Additionally, I need help with ...

Bootstrap: Problem arises when columns do not total up to 12 and are pushed onto separate rows

So, I've been working with Bootstrap to structure columns and rows. It seems like I have the container, rows, and columns set up correctly for each post. However, I am puzzled as to why a new row is created for every post I make, causing them to stack ...

Access a particular html tag as a value within an object

Recently, I've been working on AngularJS version 1.6 and am faced with an API containing multiple objects structured similar to the example below: { "description": " <p><a href=\"url">link</a>text</p><p><a href ...

Having trouble installing Bootstrap using the `npm i bootstrap` command? It seems like the

The npm i bootstrap command is not working when trying to install Bootstrap. Nothing is being added to the packages.json file or node_modules directory. Here are the dependencies listed in the package.json file: "dependencies": { "@angu ...

Getting ngSanitize to function properly with the <iframe> tag

I am currently developing a solution using AngularJS, where ngSanitize is being utilized to sanitize the markup retrieved from an Ajax call. There are times when this markup may include iframe tags. During testing, I have noticed that ngSanitize appears ...

Does text-underline qualify as a property in CSS?

My stylesheet includes a property called text-underline:none, and I'm wondering if it is a valid CSS property. Can you confirm this? ...

Scaling down in CSS does not center the element properly using the margin 0 auto technique when the scaled element was larger than the container before scaling

Upon scaling down an element that previously overflowed its container, I noticed that margin: 0 auto no longer centers the element as expected. Interestingly, even using transform-origin: center center did not fix this issue. It seems that the auto margins ...

Secure an API endpoint to restrict unauthorized access

I'm working on a web application using AngularJS, Express, Node.js, and Passport.js. I have been creating API endpoints as needed, but now I've encountered a problem. There's one endpoint, /api/users/updatePoints, that should only be called ...

Is it achievable to animate dynamic height using CSS? Open to JS alternatives as well

I am currently utilizing AngularJS, which enables me to utilize ng-show and ng-hide in order to display or hide elements based on a logical condition. My goal is to animate the size changes of the container when the child objects are shown or hidden, creat ...

Error: The object does not have a method called 'to top scroller' and it is causing an uncaught type error

A plugin was obtained from the following link: http://www.jqueryscript.net/other/jQuery-Plugin-For-Smooth-Scroll-To-Top-Bottom-scrollToTop.html.i and the code was attached below in the index.html file. Several jQuery plugins were attempted, but none work ...

Ensuring content stays at the bottom of a square div in Bootstrap 4

I am currently developing a budgeting app and I am aiming to create a design with a series of tiles, similar to the one shown below. However, I've encountered an issue where the content I try to add to the tiles ends up sticking to the bottom. Using ...

Aligning Text to the Right Using CSS

HTML <body> <div class="menu_items"> <h1>My Heading</h1> <nav> <a>Item 1</a> <a>Item 2</a> <a>Item 3</a ...

Choose the initial selection from a dropdown menu using AngularJS

I am facing a minor issue where I want the first element in my select dropdown to be displayed properly rather than just a blank space. Below is the code snippet I am currently working with: <select style="border-radius: 4px;" class="form-control" ng- ...

Creating a combined Email and Password form field in a single row using Bootstrap

Recently, I decided to explore bootstrap. I am looking to implement email/password validation in one row on my rails app: email? | password? | OK Currently, I have a functioning code for displaying only an email field and an OK button in one row: email? ...

Incorporating <span> elements into a comma-separated list using Jquery on every other item

When I receive a comma-separated list of items from a database and insert them into a table cell, I want to apply alternating styles to make it easier for users to distinguish between them. For example: foo, bar, mon, key, base, ball I'm looking to ...

Which is better for parsing HTML - CSS or XPath selectors?

For my project, I aim to utilize lxml for parsing HTML content, as it offers support for both XPath and CSS selectors. I am currently deciding whether to bind my model properties to either CSS or XPath. I am contemplating which option would be most benefi ...

What is causing the nav-link items to not change to white in this dark Bootstrap 4 Nav?

Why are the nav-link colors not turning white in this dark Bootstrap 4 Nav code snippet? They should behave like Navbars, shouldn't they? <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="_ ...