What is the process for changing the background color with Angular?

It should be a straightforward task, but for some reason, it just won't cooperate.

Within my controller:

$scope.projects = [ //...
    { background: "#ffffcc" }, 
    //... 
];

In the HTML:

<div ng-repeat="project in projects" 
     ng-style="{'backgroundColor': project.background}">
</div>

Replacing project.background with '#ffffcc' does the trick. I've also attempted using background-color without success. Even trying {{project.background}} hasn't worked as expected.

Answer №1

Here is an example of how you can achieve this:

<div ng-init="designs=[{backgroundColor: 'blue', width:'120px', height: '50%', display: 'inline-block'}]">

<div ng-repeat="item in designs" ng-style="item">
.example {
    background-color: {{ item.backgroundColor }};
    display: {{ item.display }};
    width: {{ item.width }};
    height: {{ item.height }};
    }
</div>

If you prefer to set the styles in your controller, you could do it like this:

$scope.designs= [{
    backgroundColor: 'blue',
    width:'120px',
    height: '50%',
    display: 'inline-block'}];

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

Issue encountered when attempting to locate the file path within the getStaticProps function of an internal page in Next Js

I'm currently implementing a multi-language feature on my Next JS app. Below is the folder structure of my project. https://i.stack.imgur.com/5tD2G.png On the Home page (index page), I successfully retrieved the locales module using getStaticProps: ...

Retrieving data from JSON attributes in a REST API response

When using the 'get' function in the 'request' node module, I am attempting to access the 'items' array in the response I receive. Despite being able to log the entire response to the console, I encounter an issue when trying ...

I have a question about using Jquery/JS. If I modify an element's HTML, will I still be able to execute other Jquery/JS actions on it?

I have a script that, upon clicking, initiates an ajax call to connect to the database, retrieve the image name, set it inside an <img> tag with the correct path, add a hidden checkbox after it, and then echo it. After receiving the ajax message, I ...

React Native formInput Component with Underline Decoration

Utilizing the FormInput element in React Native Elements, I have observed a line underneath each FormInput component. Interestingly, one line appears fainter than the other. https://i.sstatic.net/ZD8CI.png This is how the form looks: <View style={sty ...

Arrange elements within a division when the division splits into two rows

Currently in my setup: I have a navigation bar with a group of buttons located on the right-hand side. When the navbar is reduced to a smaller size, like on a smartphone, the two buttons get split across 2 rows within the div. In this state, I'm ai ...

Angular 2 has upgraded its Controller functionality by replacing it with Component

I find it a bit challenging to distinguish between Component and Controller. How was the Controller replaced by component in Angular 2? I came across this description of a component: In Angular, a Component is a distinct type of directive that utilizes a s ...

Ensure that the jQuery datepicker is set with a maximum range of 365 days between the two input fields

Setting Up jQuery Datepicker Inputs I have implemented two jQuery datepicker inputs with default settings as shown below: $("#polis_date_from").datepicker({ uiLibrary: "bootstrap4", changeYear: true, changeMonth: true, dateFormat: "yy.mm.dd", ...

The use of jQuery ajax requests is leading to a refresh of the page

I've encountered an issue with a button on my HTML page that is not associated with any form. <input type='button' id='submitter' value='add'/> There is a click handler attached to it: $('#submitter').c ...

developing a fluid grid system with flexible ordering

I'm currently working on a section for my app that consists of one row with 3 columns, each column containing 3 divs. JSFIDDLE: demo Here is how it should appear in desktop view: https://i.sstatic.net/ugAXd.png And here is the expected appearance ...

Sorting and filtering data using AngularJS filter and orderBy inside a controller or factory

I am currently working with a factory that looks like this: app.factory("ModuleFactory", function (api, $http, $q, filterFilter) { var moduleList = []; var categoryList = []; var moduleTypeList = []; var academyModuleTypeList = []; var mostUsed = []; var ...

Enhance the user experience by adding visual appeal to the first option in the selection form. Make it

Is there a way to change the color of the first option in the selection box to grey, similar to the example above? Additionally, how can I create a clickable icon that will display all options? The current setup is not functioning correctly. <div clas ...

Combining intersecting objects with interval attributes in an array using javascript

I have encountered similar questions, however, the provided answers do not resolve my specific issue. The data I have is an array of range objects, each with the following properties: start: Integer, indicating the start of the range, end: Integer, indi ...

Set the value in ng-model as the initial value in ng-init

My issue involves a select field and an 'input' field, both of which are populated with data from a database. The selected option in the dropdown is automatically checked based on the value retrieved from the database. When I change the dropdown ...

What is preventing my div from reaching 100% height when I implement an HTML5 doctype? Is there a solution to achieving a full height div?

I'm currently working on organizing the layout for a basic gallery webapp, and I've encountered an issue where some of my divs are shrinking in height when using an HTML5 doctype declaration. Despite trying different CSS properties, I can't ...

Guide to changing the background colors of multiple elements when hovered over by the mouse?

I want to customize my website's search bar by changing the background color when it is hovered over. Currently, the search bar has two main elements: the text box and the submit button. I have successfully programmed the text box element to change to ...

How to ensure the table fits perfectly on the screen using JQueryMobile and Cordova?

I've been working on a JavaScript function that creates a dynamic page and displays data fetched from a database. However, I've encountered an issue where the displayed data overflows past the width of the mobile screen. Is there a way to ensure ...

Mastering the art of column stacking with CSS on your WordPress website

Looking for a CSS solution to adjust the layout when the screen resolution is lowered. Currently, I have two rows with three columns in each row, containing blurbs. My goal is to make these two rows convert into three rows with two columns each at a cert ...

Retrieve a file from an AWS S3 bucket using AngularJS

Currently utilizing angularjs. I am in need of incorporating a download feature. <button class="btn btn-labeled btn-info" title="download"> <a href="link provided by s3" download="downloaded">Download</a> </button> I have ...

Utilizing getUserMedia to capture portrait shots with the back camera

I am encountering an issue with starting the back camera in portrait mode using navigator.mediaDevices.getUserMedia in React. The camera does not appear to be taking into account the constraints I have set. Below is how the code looks for initialization: ...

Creating a Next.js dynamic route that takes in a user-submitted URL for customization

Currently, I have implemented the Next.js Router to facilitate the display of different dashboards based on the URL slug. While this functionality works seamlessly when a button with the corresponding link is clicked (as the information is passed to the Ne ...