Show the background color on each list element that is being created from the ng-repeat directive

This code dynamically displays a list of li's with images and text.

<ul class="presale">
                    <li ng-repeat="(k,v) in value | groupBy : 'presales_name'" ng-click="showData(k.split(' ').join('-').replace('(','').replace(')',''), $index)">
                        <div class="imgDiv">
                            <img src="/images/phones/sub-family/xs/{{k.split(' ').join('-').replace('(','').replace(')','-').toLowerCase()}}.png">
                        </div>
                        <p><a>{{k}}</a></p>
                    </li>
</ul>


$scope.showData = function (item, index) {              

}

I am looking to enhance this code by adding functionality that allows the user to select a li item, which will change its background-color, while also removing the background-color from any previously selected li items.

Answer №1

If you want to implement something similar, you can follow this approach:

JavaScript File

var app = angular.module('app', []);

app.controller('NavigationController', function ($scope) 
{
    // Using a wrapper object is necessary for "activeItem" to work correctly
    $scope.states = {};
    $scope.states.activeItem = 'item1';
    $scope.items = [{
        id: 'item1',
        title: 'Home'
    }, {
        id: 'item2',
        title: 'Public Rooms'
    }, {
        id: 'item3',
        title: 'My Rooms'
    }];
});

HTML File

<body ng-app="app">
    <div ng:controller="NavigationController">
        <p>
            Your active menu item: <span class="highlightOne">{{states.activeItem}}</span>
        </p>
        <ul class="navigation">
            <li ng-repeat="item in items" class="item" ng-class="{'active': item.id == states.activeItem}" ng-click="states.activeItem=item.id">{{item.title}}</li>
        </ul>
    </div>
</body>

CSS File

ul.navigation li
{
    background: #f6f6f6;
    padding: 10px;
    margin: 2px 0;
    display: block;
    cursor: pointer;
}

    ul.navigation li.active
    {
        background: blue;
        color: #f6f6f6;
        display: block;
        padding: 10px;
    }

.highlightOne
{
    background: orange;
    font-size: 125%;
    margin: 5px;
    padding: 5px;
}

.navigation
{
    display: block;
    background: #666;
    margin: 10px;
    padding: 10px;
    width: 40%;
}

p
{
    margin: 10px 0 20px;
}

To see the code implementation in action, refer to "https://codepen.io/SkiWether/pen/lzjAq".

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

Refreshing a single div with a variety of potential hyperlinks

My knowledge of jQuery is limited, but I recently discovered a code snippet that should allow me to refresh only a specific div on my webpage: <script> $(document).ready(function(){ $(".reload").click(function(){ $("#day").load("/some_url #da ...

Unable to set width for td element in media query is not functioning as expected

Is there a way to adjust the width of td elements for smaller screens within an email template? I have tried setting the style as important, but it doesn't seem to be working. CSS .alignmentColumn { width: 25% !important; //for desktop @med ...

Is there a way to load a CI controller function in view without having to refresh the page

Whenever I want to view a specific league, I need to retrieve the seasons associated with that league from the database. This is the code I attempted to use: test.php view <script type="text/javascript"> $(document).ready(function () { ...

How do you populate a dropdownlistfor in ASP.NET MVC after a form

My issue is that <form> @Html.DropDownListFor(x => x.City, provinces, "--Select City--", new { @class = "dropdownList" }) @Html.DropDownListFor(x => x.district, Enumerable.Empty<SelectListItem>(), "--Select district--") < ...

Tap the mobile menu button twice to toggle the menu

Incorporating: Visual Studio 2013, ASP.NET (VB) Webforms. Upgrading an existing WebForms website from .NET 2.0 to .NET 4.5 and implementing mobile-friendly features for better Google support. I have added a button to the top right corner as follows: < ...

What could be causing the compatibility issue between fr units and grid-template-rows in CSS grid?

I'm having trouble getting the row height to change using fr units in my grid-template-rows. It seems to work fine with other units like pixels, but not with fr. I find it confusing that the fr units work perfectly fine with grid-template-columns, so ...

Utilize jQuery to perform polling on a web service

I am looking to periodically check a webservice for new messages every hour using jQuery. The ajax call I have in mind looks like this: function checkForMessages(rowid) { $.ajax({ type: "POST", url: "MyPage.aspx/CheckForMessages", contentType: ...

Height CSS does not conceal side bar hyperlinks

CSS .nested-menu { .list-group-item { cursor: pointer; } .nested { list-style-type: none; } ul.submenu { height: 0; } & .expand { ul.submenu { ...

What is the best way to retrieve the items stored within the array generated by a JavaScript function?

This particular function is responsible for calling a PHP script that outputs JSON data. It then iterates through the JSON object, creates a new object, and adds this new object to an array which is ultimately returned by the function. function getTestQues ...

JavaScript: displaying the value of a checked checkbox

Here is the HTML code snippet I am working with: <form id="my form"> <input type="checkbox" name="interest" value="swim"/> <input type="checkbox" name="interest" value="baseball"/> <input type="checkbox" name="interest" value="basketb ...

Utilizing the value selected in a form to trigger an action method without the use of a query

I am currently utilizing pug to generate pages for my nodeJS server. In this particular case, I am attempting to use the value selected in a form select option to dynamically change the action method. Essentially, the goal is to utilize the group name sel ...

Uploading images dynamically using Ajax and PHP upon submission

Currently, I am working on a modal that submits information and an image through a form to a database using PHP and JavaScript. However, my expertise in JavaScript is limited, so I could use some assistance. So far, I have successfully inserted data from ...

Click event dynamically bound to list items

I have a web application built with Durandal and Knockout. Here is the HTML code snippet: <ul id="header"> </ul> In a JavaScript function, I am dynamically adding a list item as follows: $("#header).append('<li id="btn"><a hre ...

Using jQuery to change date format from mm/dd/yy to yyyy-dd-mm

I need to transform a date in mm/dd/yy format from an ajax response into yyyy-mm-dd format using jquery. Is there a built-in function in jquery that can handle this conversion for me? ...

User is automatically logged in upon account completion

Is there a way to improve user experience in my app by implementing Firebase authentication so that: Users do not need to log in again after creating an account Users are automatically logged in after creating an account To achieve this, I am follow ...

Enable/Disable Button Link Tag

Utilizing AJAX, I have successfully managed to change the flag status to Active or Deactive. My goal now is to disable the button if the flag status is set to 1 or 0. However, I am facing an issue with the 'if clause' as the entire button is bein ...

The HTML code for the base64 image conversion failed to load

I am facing an issue with handling HTML strings containing base64 image tags generated by Summernote. I have a process in place where I load the image elements, convert them to image blobs, upload them to get the image-url, and then update the src attribut ...

When applying a maximum width of 100% with automatic height, images may become cropped

I'm a beginner in learning CSS, and since my technical skills are limited, I'll try to keep things simple. Currently, I have an image taking up half of the screen with the following CSS code that I found after reading some articles: <div class ...

D3 group of legendary elements

Is there a way to group both the circle and text elements for each legend item together? I'm currently facing a challenge with the enter/exit methods. While I can create the groups (g), I'm unable to append the text and circle elements. li.sel ...

Using Flask and jQuery, learn how to toggle the visibility of a reply text box

After a break from using HTML/JavaScript, I find myself a bit puzzled about how to handle this. I'm working on creating a reddit-like clone with Flask. I've reached a point where I can add child comments to any comment and have them display nic ...