How to apply background-color to a div element with ng-repeat directive?

Hey there, I'm dealing with the following code:

angular.module("myApp", []).controller("myController", function($scope) {
  $scope.boxList = [{
    color: 'red'
  }, {
    color: '#F8F8F8'
  }, {
    color: 'rgb(50, 77, 32)'
  }];
});
.box {
  width: 15px;
  height: 25px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myController">
  <div ng-repeat="box in boxList" class="box" ng-style="{'background-color': box.color}"></div>
</div>

I want to iterate through the boxList using ng-repeat and assign different background colors (text, hex or rgb) to my div boxes. There should be three boxes each with a unique color. I tried using ng-style as suggested by some examples on Stack Overflow, but it doesn't seem to work. Any thoughts on what might be going wrong? Thanks.

UPDATE: SOLUTION ->

ng-style="{'background-color': box.color}

The background-color needs to be enclosed in single quotes for it to work properly.

Answer №1

Here is the corrected code:

angular.module("myApp", []).controller("myController", function($scope) {
  $scope.boxList = [{
    color: 'red'
  }, {
    color: '#F8F8F8'
  }, {
    color: 'rgb(50, 77, 32)'
  }];
});
.box {
  width: 15px;
  height: 25px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myController">
  <div ng-repeat="box in boxList" class="box" ng-style="{'background-color':'{{box.color}}'}"></div>
</div>

You had some syntax errors which I have corrected. Make sure to use interpolation ({{variable}}) for rendering values.

The only change I made was

ng-style="{'background-color':'{{box.color}}'}"
for your reference. Best of luck! ;)

Answer №2

<div ng-app="myApp" ng-controller="myController">
      <div ng-repeat="item in itemList" class="item" style="color : {{item.color}}">
</div>

I believe this will work

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

I wish to have the option to choose a courseId when creating a new hole by clicking the "create new hole

I'm currently developing a golf score tracking application. I have set up a database with tables for Rounds, Courses, and Holes. The Hole table contains a foreign key that links to the CourseId, allowing me to associate each hole with a specific cour ...

Why is the session variable in PHP failing to store the value?

Could you assist me with the following PHP code snippet? I'm trying to ensure that when the update function is called, the session variable 't' increments its value. However, every time I run the code, the output remains at Value: 1. What ad ...

The jQuery UI Datepicker does not seem to be functioning properly within the Angular JS UI Bootstrap modal

I am facing an issue where I can't seem to open the jQuery UI Date Picker in UI Bootstrap Model. The normal HTML date picker works fine, but the jQuery Date Picker does not open up. Below is the code snippet: Test.html <!DOCTYPE html> <html ...

Retrieve data from a JSON gist by parsing it as a query string

I have a JavaScript-based application with three key files: index.html app.js input.json The app.js file references input.json multiple times to populate content in div elements within index.html. My goal is to enhance the functionality so that when acc ...

Tips for creating a mandatory textarea input field

It's pretty straightforward - I have a textarea that is set to required, but it only alerts the user if you actually click inside the text area. If you try to submit without clicking inside, it won't prompt the alert. Take a look at this Fiddle ...

Implementing modifications to all HTML elements simultaneously

In my HTML document, there are around 80 small boxes arranged in a grid layout. Each box contains unique text but follows the same structure with three values: name, email, and mobile number. I need to switch the positions of the email and mobile number v ...

Radio buttons failing to transfer any data

I am experiencing an issue with setting values for radio buttons and using the $_POST variable to read the value, but the value being set is empty. <form style="text-align:left;margin-left:80px;" class="form-inline signup" role="form" method="post" act ...

Avoiding the use of mailto links in PHP

One problem I encountered is with a mailto link in my PHP code: <a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3f5a525e56537f5b50525e5651114b535b">[email protected]</a>?subject=<?php rawurlencode ...

Achieve a stunning visual effect by placing images behind the background using HTML and

I'm currently working on developing a webpage with a slideshow feature. I've decided that I want the transition between images in the slideshow to be a smooth fade effect, going from color to image to color. To achieve this, I have set up a backg ...

Obtaining the jqXHR object from a script request loaded within the <head> tag using the <script> tag

Is it possible to retrieve the jqXHR object when a script loaded from a script tag within the head tag? function loadScript(url){ const script = document.createElement("script"); script.src = url; // This line will load the script in the ...

Tips for sending multiple Arrays from AngularJS to Spring Controller

Currently, I am facing a challenge in passing multiple lists from AngularJS to a Spring controller using a POST call. While sending just one list works fine with the Spring controller, I am unsure about the best practice for sending and handling multiple l ...

Executing PHP code from a list item

On one of my website pages, I have a list that functions as a delete button. However, I am interested in making it so that when a user clicks on the delete option, a php script is triggered - similar to how a submit button works. Below is the list structu ...

How about: "Insert a line break inside a div element"

My goal is to have 3 icons positioned side by side that will float left if the window shrinks. Under each icon, I plan to include some text. Below you can see that I've made progress on this layout. .icons { BORDER-TOP: black 1px solid; HEIGHT: 10 ...

Maintaining data consistency across various screens using the ui-router functionality

Encountering difficulties with maintaining the item quantity state in the Main Controller View after navigating back from the Cart Controller View. While adding 2 items in the Main Controller View reflects correctly in the Cart Controller View, returning t ...

Tips for displaying an array and iterating through its children in Angular 7

I am currently working on extracting the parent and its children to an array in order to display them using ngFor. However, I am encountering an issue where the children are not being displayed during the ngFor. I have a service that retrieves data from a ...

Establishing online payment through PayPal

I'm currently working on a project that involves the integration of web payment as a new feature. I am excited to mention that our client, who is quite tech-savvy, recommended using PayPal for this purpose. This will be my first time working with web ...

Is it possible to declare a variable as both $rootScope and $scope within the same controller?

Is it possible to declare a variable as both $rootScope and $scope in the same controller? $scope.Account=[{'sdfdsf','sdfds'}]; $scope.refreshAccountSummary = function () { Report.account(function (res) { $rootScope.Account ...

ImageMapster for perfect alignment

I'm struggling with centering a div that contains an image using imagemapster. When I remove the JS code, the div centers perfectly fine, indicating that the issue lies with the image mapster implementation. It's a simple setup: <div class=" ...

What is the reason for the lack of an applied CSS selector?

.colored p{ color: red; } article > .colored{ color:powderblue; } .blue{ color: white; } <!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8> <meta name="viewport" content="width=device-width, initi ...

Express not automatically displaying the index.html file as a static file

In the structure of my directory, I have: build/ (contains gulp tasks for generating dist from src) dist/ (this is the static app that is being served) (all other assets, css, images, etc) index-5c1755df65.html src/ (source files that cannot be serv ...