The Angular ng-style feature is responsible for applying a style attribute that is subsequently not maintained in proper sync

My angular website retrieves data from a Web API as its back end. Initially, a generic background is displayed. However, after the user logs in, the background image URL specific to the customer is fetched and utilized in the ng-style attribute.

Upon the page's initial loading, an unexpected style attribute is present, directing the background to the default image, even though it's not in the source code.

Despite updating the ng-style to reflect the correct background image URL after login, the style attribute continues to point to the default URL.

Upon manually refreshing the page (with no cache in the Chrome developer settings), the style attribute is finally updated, and the customer's background is correctly shown.

Why is Angular adding a style attribute that fails to stay in sync with the ng-style property? What could be causing this issue?

Source Code

<div class="background-div" ng-style="{'background-image': 'url({{user.profile.Customer.BackgroundImageUrl || '../images/bg.jpg'}})'}"></div>

Initial page load

<div class="background-div" ng-style="{'background-image': 'url(../images/bg.jpg)'}" style="background-image: url(http://localhost:10223/images/bg.jpg);"></div>

After login

<div class="background-div" ng-style="{'background-image': 'url(http://megacorp.com/images/vipcustomer.jpg)'}" style="background-image: url(http://localhost:10223/images/bg.jpg);"></div>

After forced no-cache refresh

<div class="background-div" ng-style="{'background-image': 'url(http://megacorp.com/images/vipcustomer.jpg)'}" style="background-image: url(http://megacorp.com/images/vipcustomer.jpg);"></div>

Fixed Code

  <div class="background-div"
       ng-style="{'background-image': 'url(' + (user.profile.Customer.BackgroundImageUrl || '../images/bg.jpg') + ')'}">
  </div>

Reopen:

In my opinion, this question sets itself apart by focusing on HTML and avoiding delving into $digest and script. It illustrates the different stages of the page's behavior in various scenarios. However, I leave the final judgment to you :)

Answer №1

It is recommended that ng-style does not have {{}} interpolation within it, as it is better to use direct scope variables.

Example

<div class="background-div" 
   ng-style="{'background-image': 'url('+user.profile.Customer.BackgroundImageUrl || 
   '../images/bg.jpg'+')'}">
</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

Unable to retrieve this information using $http callback

I am currently working with angular 1.5 and typescript, but I am facing an issue where I cannot access the 'this' property from the callback returned by the $http promise. Whenever I try to access a private method from the callback, 'this&a ...

Conceal the div element if the value is either undefined or empty in AngularJS

I am experiencing an issue where I get 2 different values when I use console.log($scope.variable). The values shown are undefined and ''. Based on this value, I need to hide a div. Here's what I have tried: <div ng-hide="$scope.variable ...

Tips for positioning a navigation bar in the center for various screen resolutions

After hours of searching, I still can't find a solution to my problem. I'm new to this, and on my main monitor, everything looks centered just fine. 1920x1080: View larger image But on my 1280x1024 monitor: View smaller image As you can see, i ...

What is the best way to create a drop-down menu that exports data directly to an Excel spreadsheet?

I have been struggling with a seemingly simple issue - I can't seem to get my drop-down box to display the chosen option. The code I'm using is quite similar to this generic one, but for some reason, it's not reporting the selected option to ...

Working with Parse.com's REST API: Implementing skip and limit queries using JavaScript

Currently, I am in the process of retrieving data using Parse through the REST API in Javascript. Everything is working fine until I attempt to include a limit and an offset ("skip"). Here, you can find the documentation for using parameters with a REST ...

Tips on creating a slow and gradual border animation that unfolds smoothly

I am looking to create an animation effect on a border, gradually revealing it like in this Codepen example. However, my specific requirements are: The previous line should not be removed, but rather shown along with the new border. The border color ...

Is it possible for .getElementByClassName to identify Ajax.GET elements?

I've run into a problem that I need help with: The issue arises when I have an API that makes an Ajax GET request. In the success function, it creates a button, a div, and several span elements with information inside, each with its own class. The GE ...

The conversion of string to number is not getting displayed correctly when using console.log or document.write. Additionally, the concatenated display is also not functioning as intended

Being new to JS and HTML, this program was created to enhance my understanding of the concepts. I attempted a basic program to convert a string to a number in three different ways, but I am having trouble determining if the conversion actually took place. ...

"Customizable rectangular container with jagged edges created with Scalable Vector Graphics

Currently, I am undertaking a small project that involves creating a box with rough edges around some text. To achieve this effect, I am utilizing an SVG with unique edges similar to the design found at this link: (except mine is in SVG format). My goal ...

A photograph displayed within a container using Bootstrap, positioned on the right-hand side

I'm facing an issue with my container setup that includes row > col > img. I need the image to be positioned close to the page border. So far, I have tried making the site responsive using margin-right: -n px, but it doesn't work on mobil ...

What is causing the Jquery form submit event to not trigger?

I am attempting to use jQuery to submit a form. I need the form submit event to be triggered when jQuery submits the form. However, the submit event handler is not being called successfully after the form is submitted. Here is the code snippet: <html ...

dynamically adjust table cell width based on number of rows

My HTML structure is as follows: <table border=1 > <tr> <!--this tr has one <td> that needs to be 100% width--> <td></td> </tr> <tr> <!--this tr has two <td> that each need to be ...

Are there any risks in combining MVVM [angularjs, knockout.js] with MVC [JSF/spring]?

What are the benefits of combining MVVM models like AngularJS and Knockout.js with MVC frameworks like JSF or Spring? Is it considered best practice to manipulate MVVM objects and variables from within an JSF page? For example, updating a dropdown list ...

Tips on hovering over information retrieved from JSON data

Within my code, I am extracting information from a JSON file and placing it inside a div: document.getElementById('display_area').innerHTML += "<p>" + jsonData[obj]["name"] + "</p>"; I would like the abi ...

Renewing individual parental resolution cache

Is there a way to update specific resolves in the parent resolves without refreshing the entire page? Can resolve caching be disabled for certain items in ui-router? In my current setup, the company object is passed to 2 child states by an abstract parent ...

I cannot locate the dropdown list anywhere

As a beginner in html and css, I decided to follow a tutorial on youtube. The tutorial focuses on creating a navigation bar with dropdown menus using html and css. I wanted the names Ria, Kezia, and Gelia to be displayed when hovering my mouse over the Su ...

Angular Editable Pop-up

I have implemented Xeditable in my AngularJS application to allow users to modify table values on the fly without the need for buttons. I use the blur attribute to quickly submit changes inline. <a href="#" buttons="no" blur="submit">{{ myValue }}&l ...

Discussing a non-existent web address

Is it possible to generate a URL that appears to exist but actually doesn't? Take, for example, https://twitter.com/i/notifications - this link functions as expected. However, if you delete the /notifications segment from the URL, you end up with http ...

How to adjust the timezone settings in PHPMyAdmin on a shared server platform

I'm having trouble changing my timezone to India on my shared server database. I've tried everything but can't seem to get it to work. My website is built using PHP Codeigniter The contact us page on my site saves all inquiry details to my ...

Ensure the form is validated using AngularJS along with an Ajax request

I need help with HTML code <input type="text" name="username"> <label for=""> Email </label> <input type="email" name="email"> My goal is to use AJAX to check if the username and email already exist in the database ...