Intermittent issues with ng-style functionality

I'm currently utilizing ng-style to dynamically change the background color of a container class (from a third party tallinn.css file). The color is fetched from the database. However, when I navigate through Angular's routing, the ng-style attribute on the container stops functioning and does not load anything into the inline style of the element. Interestingly, I inserted a div inside the container div and set its background color using the same ng-style and scope variable, and it consistently works. It seems to work properly after a hard reload and clearing the cache. A closer look at Chrome's debugger reveals what happens when it fails: Debugger

Upon inspection, the style tag on the "container" div appears empty while the style tag on the inner div is filled with the correct background color rgb(65,87,251).

Here is the code snippet:

//inside angular controller
function setStyles(colors) {
  //set theme styles
  if (colors) {
    $scope.styles = {
      menuBackground: colors.Menu.Background.HexCode // "#4157FB"
    };
  }

}
//inside tallinn.css
.navigation .container {
  background-color: #000000;
  bottom: 0px;
  padding: 40px 40px;
  position: absolute;
  top: 0px;
  width: 300px;
  z-index: 1000;
}
<div class="container" ng-style="{'background-color': styles.menuBackground}">
  {{styles.menuBackground}}
  <div ng-style="{'background-color': styles.menuBackground}" style="height:200px; width:200px"></div>
</div>

I've experimented with different methods to resolve this issue, like using a directive (which had the same problem), before switching to ng-style.

Could this possibly be a timing inconsistency between when the CSS loads versus when ng-style should fetch the style from the JavaScript?

Any assistance would be greatly appreciated!

EDIT:

To clarify, the dynamic color for the 'background-color' is retrieved from the database. This is why I'm utilizing ng-style instead of hardcoding it in the CSS. Ideally, ng-style should take precedence over the 'background-color' defined in the tallinn.css shown above because it should apply styles inline. However, it doesn't seem to be doing so...

Answer №1

If you're having issues, here's a workaround that may help:

<div class="container" ng-class="{'backColorMenu': true}">
  {{styles.menuBackground}}
</div>

In terms of CSS...

.backColorMenu{
  background-color:#4157FB;
}

Let me know if this solution doesn't work! Best of luck

EDIT

Based on your comment indicating the hexacolor is not static, you can try this approach:

Controller:

$scope.styles = {
background-color: "#4157FB" //May need quotes or not.. Not sure
}

HTML:

<div class="container" ng-style="styles">
      {{styles.menuBackground}}
    </div>

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

Utilizing the Bootstrap grid system to dynamically adjust column width based on specific conditions

When working with HTML and bootstrap (3.7) CSS, I often come across this issue: <div class="row"> <div class="col-xs-6">Stretch ME!</div> <div class="col-xs-6">Conditional</div> </div> Sometimes the "Conditional" d ...

Efficiently sending a cookie with an Axios POST Request

My request is not receiving a cookie even after trying various solutions like withCredentials. I have pasted the most recent code here, can anyone spot what might be missing? var cookie_for_data = "token=test"; var host = "http://localh ...

Getting hold of HTML elements in a div on a different page

Recently diving into the world of html/javascript, I find myself engaged in a project where I'm loading an external html page within a div. The loaded content looks like this: <div class="content" id="content"> <object ty ...

Tips for showcasing two pieces of content next to each other across the entire webpage using a combination of Bootstrap, CSS, and Laravel

I am facing a challenge with displaying two contents side by side on a full page and stacking them vertically when the screen size is small (mobile). I have managed to achieve the side by side display on a full page, but they stack on top of each other on ...

The use of React input sliders with Hooks is a powerful

Currently, I am working on creating an input slider using React Hooks that shows the number corresponding to the slider's position. However, I have encountered a problem where the slider is not responsive when moving backwards and is very slow to resp ...

Sending a JavaScript variable through a jQuery and HTML form

My current issue involves a list of images within a div: <div id="holiday"> <ul> <li><img src="images/stationery/christmas.jpg" width="68" height="80" /></li> <li><img src="images/stationery/easter.jpg" wi ...

jQuery remove highlighting only if it is currently applied

I am in need of a simple solution to remove the selected class when clicking on an already highlighted button. I want only one button to be highlighted at a time. http://jsfiddle.net/7wy7sjm5/1/ The reason why I first remove the class for all buttons is ...

scroll both horizontally and vertically

I came across a tutorial on creating a scrolling page using jQuery and I followed it successfully for horizontal scrolling. However, I'm now wondering if it's possible to implement both horizontal and vertical scrolling on the same page. For in ...

Combining and sorting cursor data from multiple collections based on date

Can you combine two or more different collections and arrange them by createdAt? For example, suppose we have data like this: CollectionA.insert({ createdAt: new Date(), field1: value1, field2: value3, field3: value2, ...

Gather data from a variety of HTML5 video seminars

I am encountering an issue where I have multiple videos and I want to receive events from each of them. However, I am only able to get the event from one video (as tested in Chrome). Why is this happening? Here is the HTML code: <video id="video1" pre ...

The issue arises when attempting to use Jquery trigger() within the document's readiness

Despite trying numerous variations, I have been unable to get this code to work properly. <script type="text/javascript"> $(document).ready(function(){ $('#slide_click').trigger('click'); }); </ ...

Maximize the YouTube video for full screen viewing

Take a look at the code snippet from our page here: <div className={styles.videoPreview}> <iframe src={`${videoData[router.locale]}?autoplay=0`} title="Flytime" ...

opacity of highcharts heatmap data points reduces to zero

Recently, I integrated a Heat Map feature from the Highcharts library into my app. However, I encountered a strange issue where certain rows of data were not displaying on the map. Please refer to the screenshot below for reference: Upon inspecting the el ...

Implementing a Scroll Bar within a Stack Component in Material UI

I've developed a component and now I'm looking to enable scrolling when it expands beyond the screen width <Stack direction="row"> <Stack gap={1} overflow="auto"> {fields.map((el, i) => ( ...

What is the best way to trigger b-tab based on a specific condition?

Hey there, I'm just starting out with Vue3 and I have a bit of a challenge. I've got an SVG image with different parts, a data variable that changes when a particular part of the image is clicked, and some tabs (b-tabs). Is it possible to automat ...

Are the stylesheets on my computer also applicable to Chrome?

For instance, an anchor tag by default has specific styles such as being blue, turning purple when visited, and changing the cursor when hovered over. So, where exactly does Chrome pull these styles from? Could there be a Google Chrome style-sheet stored ...

Using MaterialUI to create a GridListTile with two IconButtons

I'm working with a GridListTile and trying to add a second button, but I'm having trouble getting both buttons to display. Even though I've attempted to include two ActionIcons, only one of them is showing up. Here's the code snippet: ...

Luxon: retrieve an array of time offsets and time zones (similar to what can be done in moment-timezone)

Currently, I am using the moment-timezone library to retrieve raw information for a specific timezone and then incorporating it directly into my downstream code. const zone = moment.tz.zone('Europe/London'); This data contains: { "name":"Eu ...

The step-by-step guide to implementing async/await specifically for a 'for loop'

Is there a way to make 'submitToTheOthers' function run after 'let items = []' has completed, without needing an await within 'submitToTheOthers'? I am considering using await within the for loop in 'submitToTheOthers&apo ...

Encountering a syntax error when attempting to generate a div dynamically with jQuery

I am in the process of creating a view application form using Bootstrap after submitting a form. Initially, I created it utilizing two 'div' elements. Now, I am exploring how to dynamically generate a div upon clicking a button: <i> Sectio ...