Customize the background color of each item in an Ionic framework list

Is there a way to create a different background color for each list item using Ionic? For instance, if the list includes fruits like banana, apple, and orange, can we set the background color to yellow for banana, green for apple, and so on?

I've attempted to use ng-style and ng-class but haven't been successful in achieving the desired outcome. I am using collection-repeat for the list.

Any suggestions on how to accomplish this would be greatly appreciated!

EDIT:

http://plnkr.co/edit/L80IcehgBQTiVXCCLWo9?p=preview

HTML

<!DOCTYPE html>
<html ng-app="myApp">
<head>

  <script src="http://code.ionicframework.com/nightly/js/ionic.bundle.js?4"></script>
  <script src="script.js"></script>
  <link rel="stylesheet" href="http://code.ionicframework.com/nightly/css/ionic.css">
</head>
<body ng-controller="MainCtrl as main">
  <ion-header-bar class="bar-positive">
    <h1 class="title">1000 Items</h1>
  </ion-header-bar>
  <ion-content>
    <ion-list>
      <ion-item collection-repeat="item in main.items" ng-class="item == '0' ? 'classA' ">
        {{item}}
      </ion-item>
    </ion-list>
  </ion-content>
</body>
</html>

JS

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

myApp.controller('MainCtrl', function() {
  this.items = [];
  for (var i = 0; i < 1000; i++) this.items.push(i);
});

CSS

.classA { 
    background-color: black;


}

Answer №1

There seems to be an issue with your ng-class expression.

The correct syntax should be

ng-class="item == '0' ? 'classA' : ''"

Additionally, make sure you have included the style.css file in your index.html:

<link rel="stylesheet" href="style.css">

You can check out the solution on this plunker link.

Answer №2

Check out this snippet:

CSS

ion-item[isEven='true'] > div.item-content{
    background-color: green !important;
}

ion-item[isEven='false'] > div.item-content{
    background-color: purple !important;
}

HTML

<ion-item data-repeat="element in main.elements" isEven="{{$even}}">

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

Having trouble with setting CSS style in <p> tags within Codeigniter framework

Why is the class mentioned in the p tag not applying in Chrome but working in IE and Firefox? <p class="p_error"><?php print $this->validation->errorValue;?></p> Here is the CSS -> .p_error{ color:red; text-align:left; font-s ...

When I try to scroll on my mobile device, the element is forced to reload

My donate page has a header with a generic placeholder image. On mobile devices like iPhones and Androids, the page initially looks great. However, when I scroll down and then back up, the header disappears for a moment before reappearing as originally ren ...

can a table tag without a specified width still be successfully centered?

While learning a trick to center an element, I came across an interesting method. The suggestion was to place the ul element inside a table and set the table's margin left/right to auto in order to center it. However, I found myself wondering why the ...

Images that are not aligned to the left

I'm really struggling with this issue that seems so simple. I am trying to create a grid layout of images like the following: [Image] [Image] | [ Big [Image] [Image] | Image ] Creating the grid with just images is not difficult for me, but th ...

Alignment of cells in a table

This problem is really bugging me. I've done my research and never had trouble with this before, but for some reason, I can't get the second div to display on the right side of the table cell. Any assistance would be greatly appreciated. Here is ...

I have a desire to use Javascript to control CSS styles

Within the CSS code below, I am seeking to vary the size of a square randomly. This can be achieved using the Math.random() property in Javascript, but I am uncertain how to apply it to define the size in CSS. #square { width: 100px; height: ...

The X-axis scrollbar is cleverly tucked away at the bottom within the vertical scroll

My overflow-x scrollbar appears hidden behind the overflow y scrollbar. Is there a solution to resolve this issue? The height and width are currently derived from the parent .wrapper div, but despite being commented out, the overflow-x property seems to d ...

Animation of drawing lines in SVG in reverse

Does anyone know how to reverse the direction of this SVG line animation? I want the line to draw from left to right instead of right to left, but I'm having trouble figuring it out. Adjusting the end point in Illustrator caused it to draw from both s ...

What is the best way to eliminate the curved border and inset box shadow from the Material UI TextField component in React?

Check out this input field: https://i.sstatic.net/Z6URV.png Notice the inner shadow and curved borders. Take a look at the given code snippet: import React, {Component} from 'react'; import TextField from 'material-ui/TextField'; im ...

What is the best way to improve the design of this division that includes buttons?

I'm having trouble with the layout of three divs I have set up. One acts as a container, while the other two are meant to hold buttons. However, something seems off and I can't figure out how to correct it. .button { display: inline-block; ...

Using a glass of water as a metaphor to illustrate advancements in CSS and JQuery

I've been working on a new app and have recently started implementing some significant changes. The app has a unique feature that allows users to track their water intake and compare it to their set goal. One key aspect I am struggling with is creati ...

When the border width is set to 1px in FireFox, the border size is calculated as 0.667

When using the FireFox browser alone, I noticed that the border size is displayed as 0.6667 when setting the border width to 1px on a div, input element, or any HTML element. The same issue occurs whether the border is applied using inline CSS or a separat ...

What causes the circular progress bar to disappear when hovering over the MUI React table?

My goal was to create a table with a CircularProgressBar that changes its background color from orange to dark blue when hovering over the row. However, despite my efforts, I couldn't get it to work as intended. Additionally, I wanted the progressBar ...

Utilize XML and Javascript to create a captivating donut chart

I am currently experimenting with XML and JavaScript to generate a donut chart. I attempted to implement this JS code. However, I am struggling to create XML that is compatible with the JS. var SCIENCE = 0; var ART = 0; var MATH = 0; $(document).ready ...

The jQuery "Chosen" select accordion is experiencing the issue of its height becoming too large and then snapping back into place

I have integrated jQuery Chosen with a Twitter Bootstrap Accordion. The issue of the Chosen dropdown being clipped by the accordion body has been resolved by referring to solutions provided in this thread. However, I am still encountering another problem w ...

Display endless data within a set window size

I am looking to create a fixed-size window for displaying text from the component <Message/>. If the text is longer, I want it to be scrollable within the fixed window size. See screenshot below: Screenshot export default function AllMessages(){ ...

Creative CSS spinners design

Seeking assistance to modify my spinner design. I want one end to be thicker and the other end to be thin. My attempts so far have been unsuccessful. <div id="data-loader"> <div class="loader"> <div class="blue"></div& ...

Personalizing the hamburger icon in Bootstrap 4.0

I want to modify the color of the three lines as well as the border around the icon using CSS, but I'm unsure which tags to target. CSS: .navbar-toggler .navbar-toggler-icon { background-image: url("data:image/svg+xml;charset=utf8,%3Csvg viewBox=& ...

Footer not disappearing

Need assistance! My footer isn't showing up properly at the bottom even after applying clear:both. Can someone help please? If anyone can provide guidance, it would be greatly appreciated. Thanks for all your help in resolving the issue. ...

Guide to implementing tooltips for disabled <li> elements in AngularJS

I have a list of items that are displayed using the following code: <li class="dropdown-item" data-toggle="tooltip" uib-tooltip="tooltip goes here" data-placement="left" data-ng-repeat="result in items.results.contextValues.rows " ...