What is the best way to dynamically update or display unique CSS styles when a service is invoked or provides a response in AngularJS using JavaScript

Seeking to display a unique CSS style on my HTML FORM prior to invoking a service in my code and then reverting back after receiving the response. I have implemented the use of ng-class to dynamically add the class when the boolean activeload1 is set to true. However, I am encountering an issue where this change is not reflected in my HTML. To troubleshoot, I purposely triggered an exception in the service call to observe if the class changes during that scenario, which it does (but remains stuck on the loading css due to never reaching the class deactivation following the exception). How can I ensure that the CSS class change/addition is reflected in my HTML? Currently, the code simply sets the value of activeload1 to true, calls the service, and then assigns false without any visible effects. My code:

HTML:

<div id="divPrincipal" class="wrapper" ng-class="{loading1:bravosCtrl.activeload1}"> </div>

Snippet from JS function:

    this.search = function(){            
        this.activeload1 = true;
        if (this.query){
            this.userService.searchUser(this.query, this.userSession).then((users) => {
                console.log("SUCCESS");
            });
            this.activeload1=false;
        }else{
            this.activeload1=false;
            this.error = true;
        }
    };

CSS :

.wrapper {
border-radius: 10px;
background: #24CDD1;
padding: 25px;
max-width: 680px;
margin: 0 auto;
margin-bottom: 20px;
box-shadow: 2px 8px 8px rgb(0 0 0 / 10%);
position: relative;
}

.wrapper.loading1::before {
content: '';
background: url('https://i.imgur.com/sdK5jNw.gif') no-repeat center center;
width: 32px;
height: 32px;
display: inline-block;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 9999;
}
.wrapper.loading1::after {
content: '';
background: #24CDD1;
display: inline-block;
opacity: .65;
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: 999;
}

Answer №1

When using the $http service, keep in mind that the request is asynchronous. In this code snippet, activeload1 is being set to false immediately, before the http response has been received.

To ensure that activeload1 is set to false only after receiving a response from the $http service, make sure to add it inside the success callback function like so:

this.usuarioService.buscarUsuario(this.busqueda,this.userSession).then((usuariosE) => {
     console.log("SUCCESS");
     this.activeload1=false; // place it here
});
      

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

Tips for positioning the labels in a sankey diagram for optimal alignment

When multiple values are the same in this scenario, such as 0, the labels start to overlap. Can anyone provide guidance on how to align these labels vertically? Ideally, I would like them to be positioned at the top of the node/bar. Highcharts.chart(&apos ...

Issue with running gulp ser on first attempt in SPFX

Every time I try running gulp serve, I encounter the following issue: Error: Unable to locate module '@rushstack/module-minifier-plugin' Please assist me with this problem. Thank you! ...

Sending an AJAX request from one subdomain to another subdomain within the same domain

Is it true that cross-domain ajax requests require a 'proxy' server to be used? But what if an ajax request is made from server1.example.com to server2.example within the same domain of example.com? Would that still not work? I've noticed ...

Adjusting containers for optimal display during browser zoom and resize operations

Currently, I have designed a banner using a triangle and rectangle to overlay an image. However, when the browser is zoomed in, there seems to be a gap between these two shapes. I am looking for suggestions on how to better connect or approach this banner ...

The Bootstrap 3.3 Carousel is stationary and does not rotate

I am facing a challenge with implementing a Carousel using Bootstrap version 3.3.7 on my website. The code snippet is as follows: <!-- Carousel ================================================== --> <div class="row dark-start d-none d-lg-block"&g ...

Error encountered in VUE JS 3: The function vue.initDirectivesForSSR is not recognized as a valid function

I've been encountering some errors while trying to build my Vue web app with this configuration. I have searched for a solution, but couldn't find anyone facing the same issue as me. Any suggestions on how to resolve this problem? The build was s ...

Traversing an object with a loop

I'm currently working on a project that involves utilizing the swapi co API. Although I've successfully fetched results from the website, I'm struggling with displaying only specific API objects, particularly array spaceships. var linkApi=" ...

How can I align the Socket.io path on the client with the directory structure of my Node.js server?

Currently, I am utilizing socket.io with node.js along with Expressjs. As I serve my HTML page, I have the socket.io.js file linked directly in a script tag: <script src="/socket.io/socket.io.js"></script> I'm facing some difficulty match ...

Correctly align the div on the screen as the viewport is scrolled

I am currently working on a parallax website where the sliders are designed to slide from the left and align within the viewport as the user scrolls. However, I have encountered an issue where multiple scroll actions are required for the slide to align pro ...

Determine the checked status of a checkbox with Selenium

Here is a code snippet that showcases a checkbox element and the goal is to determine whether it is checked using Selenium. for (int j = 2; j < 5; j++) { List<WebElement> td1 = row1.findElements(By.tagName("td")); WebElement check = td1.g ...

Embedding content within various ng-template elements

I'm currently working on developing a button component (app-button) that can utilize multiple templates based on the parent component using it. <div class="ds-u-margin-left--1 ds-u-float--left"> <ng-container *ngTemplateOutlet="icon">< ...

Issues with javascript and php carousel functionality not performing correctly

I am currently in the process of creating a slideshow/carousel for a website. Recently, I implemented a PHP for-loop to iterate through a folder of images, allowing me to use the slideshow with an unspecified number of images. However, after making this ch ...

The issue with Webpack chunking is that no content is appearing because the chunks are not

For the past day, I've been trying to resolve a frustrating yet seemingly simple problem. My goal is to split my bundle.js file into chunks to enhance website loading speed. Below is my webpack.config file: module.exports = { devServer: { historyApi ...

Can jQuery Autocomplete function without stopping at white spaces?

Is there a way to modify jQuery UI autocomplete so that it can handle multiple words instead of terminating with a space? For example, if you input "New York City", it should still filter results based on each word. $.ajax({ type: "GET" ...

How can you add a new div directly after the parent div's content and display it in-line?

I have a banner that stretches to fill the width completely. Inside this main div, there is another smaller div measuring 30px in size positioned at the end. The nested div contains a close icon background image, and upon clicking it, the entire banner get ...

"Is there a way to extract a specific value from an XML file based on another element

<dataset> <study recNO="0" seriesRecCount="1"> <patientID>123456</patientID> <accessionNumber>M120170428105320</accessionNumber> <patientIDID>35</patientIDID> <studyUID>2.25.1439 ...

Invoking a PHP class through an AJAX response handler code

I'm attempting to access a PHP-File using AJAX. When I use a basic PHP-File like this: <?php header('Content-Type: text/html; charset=utf-8'); header('Cache-Control: must-revalidate, pre-check=0, no-store, no-cache, max-age=0, pos ...

Is concealing content using Javascript or jQuery worth exploring?

While I have been hiding content using display:none; in css, there are concerns that Google may not like this approach. However, due to the needs of jQuery animations, it has been necessary for me. Recently, I have come across a new method for hiding conte ...

Utilizing AJAX, a PHP script creates an HTML table that allows users to easily perform actions such as deleting, updating

I'm a beginner in the world of ajax and I am eager to utilize it with php in order to develop a table that can be edited by users directly on the webapp interface. Here's my current progress... PHP class genTable{ public function table1(){ ...

Updating the progress bar without the need to refresh the entire page is

Currently, I have two PHP pages: page.php and loader.php. Loader.php retrieves data from MySQL to populate a progress bar, while page.php contains a function that refreshes loader.php every second. This setup gets the job done, but it's not visually a ...