Is there a way to display a loading indicator before or while running angular's ng-click handler?

I'm currently working on manipulating my DOM to show or hide a loading indicator while executing a controller action triggered by the ng-click directive.

This is how my code looks:

Controller:


$scope.isLoading = false;

// Set current category being listed
$scope.setCurrentCategory = function( category ) {
  $scope.isLoading = true;
  //...performing some heavy filtering operations here...
  $scope.isLoading = false;           
};

View:

<div ng-repeat="category in categories">
  <a href="#" ng-click="setCurrentCategory(category);" >{{ category.name }}</a>
  <img ng-cloak ng-show="isLoading" src="icons/spinner-mini.gif" />
</div>

The issue I'm facing is that the loading indicator only updates after the entire ng-click handler has finished, preventing the user from seeing it at all.

How can I ensure that the update of $scope.isLoading reflects in my view before the rest of the handler is executed?

Answer №1

It appears that the issue may stem from the scope digest not being invoked before the setCurrentCategory function ends.

You can manually trigger a digest after setting $scope.wip = true like this:

$scope.setCurrentCategory = function( category ) {
  $scope.wip = true;
  $scope.digest()
  //...perform expensive re-filtering here...
  $scope.wip = false;           
};

While triggering the digest within an already lifecycle function may not be optimal, it should resolve the issue.

Another approach would be to create a scope function to toggle the wip variable:

$scope.setCurrentCategory = function( category ) {
  $scope.toggleWip();
  //...perform expensive re-filtering here...
  $scope.toggleWip();           
};
$scope.toggleWip = function() {
  $scope.wip = !$scope.wip;
}

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

JavaScript powered caller ID for web applications

I am currently working on an innovative project where I aim to automatically detect the phone number of a landline call to a specific device. While research led me to various packages like npm caller-id-node that work with desktop applications using the mo ...

Sharing state between components in NextJS involves using techniques like Context API, passing

I am trying to pass state between pages in Next.js. In my App.js, I have wrapped it in a context provider like this: import { useRouter } from 'next/router' import { ClickProvider } from '../context/clickContext' function MyApp({ Compo ...

Does anyone have any firsthand experience with Internet Explorer performing poorly on a VPS?

Our team recently completed a portal project that is now live and operational. After thorough testing, everything seemed to be working perfectly until a customer used Internet Explorer with Citrix VPS, causing the page to take 12 seconds to fully load. Co ...

Storing Firestore Timestamp as a Map in the database

Snippet Below const start = new Date(this.date + 'T' + this.time); console.log(start); // Thu Sep 12 2019 04:00:00 GMT+0200 const tournament:Tournament = { start: firebase.firestore.Timestamp.fromDate(start) } When passing the tournament ...

Tips for incorporating a Spotify-style play button using CSS into a table row

Hey there! I recently attempted to customize my button like Spotify. I wanted the table row to display a play button when hovered over, and when clicked, the image would change. https://i.sstatic.net/aJIna.jpg After seeking advice from another fiddle an ...

Extension for Chrome - Personalized pop-up notification when page loads

I am looking to create a custom alert box that triggers on page load instead of the default one, which I find unattractive and possibly irritating for users. alert('hello'); My current approach involves the following code: manifesto.js "cont ...

Creating a Delicious Earth Progress Pie

I'm looking to incorporate a unique progress bar design on my website, similar to this one: The progress pie Can anyone guide me on how to create a progress pie with an image inside it that changes color as it moves? ...

Ajax: Failed to send POST request (404)

After adding a POST script in the manage.ejs file and console logging the data to confirm its functionality, I encountered an issue. Below is the code snippet: <script type="text/javascript"> var guildID = "<%= guild.id %>"; let data = {so ...

Is there a safe method to convert an HTML attribute (Javascript Object) into an array using Javascript or JQuery?

I have an HTML element containing a javascript object: <div ui-jq="easyPieChart" ui-options="{ percent: 75, lineWidth: 5, trackColor: '#e8eff0', barColor: ...

Retrieve information from an external JSON API using jQuery

I need help with reading JSON data using jQuery. I have tried the code below but it doesn't seem to be working. This is the link to my JSON file: and here is my code snippet: $.getJSON("http://goo.gl/PCy2th", function(data){ $.each(data.PlayList ...

Having trouble implementing object type switching in Typescript

While in the process of developing an angular application, I stumbled upon a peculiar issue. Some time ago, I crafted this piece of code which performed flawlessly: selectedGeoArea: any receiveStoreEvent(event) { switch (event.constructor) { ca ...

Having trouble getting your Bootstrap v4 carousel to function properly?

Currently, I have implemented the carousel feature from Bootstrap v4 in a Vue web application. I am following the same structure as shown in the sample example provided by Bootstrap, but unfortunately, it is not functioning correctly on my local setup and ...

Manipulate text with jQuery

Is there a way to remove 'http://' or 'https://' from text using javascript? I am looking for regex solutions as well. This is what I have tried so far: JSFIDDLE HTML: <div class="string"></div> JS: $text = $('.s ...

Guide to Helping Users Customize JavaScript and CSS Files before Exporting as Embed Code

I have a vision to create a unique platform where users have the ability to log in to a customized dashboard, tailor their preferences, and then generate an embeddable code for age verification pop-ups on their websites. To kickstart this project, I'v ...

What is the best way to asynchronously load an external javascript file in a popup.html file?

I have successfully implemented all the necessary functionalities, but I noticed a delay in loading the popup.html after adding an external javascript file. This file is only a few lines long, so the delay is quite frustrating. To eliminate this lag, I be ...

Bootstrap modal fails to appear on screen

Using PHP, I generate a link from the controller. The link is stored in $retailer["url"] as 0125myimage.jpg <a onClick="crop('.$retailer["url"].')" data-target="#styledModal3" href="#" class="fa fa-edit lupa"></a> Here is my JavaSc ...

Using NodeJS alongside websocket and the module.export feature

I currently have a server.js file where I have defined my routes as follows: // routes var mainRoutes = require('./routes/main.js')(app, express); var apiRoutes = require('./routes/api.js')(app, express); var socketRoutes = requir ...

Add the content script to a webpage just one time

I am looking to inject a content script on context menu click in an extension manifest version 3. I need a way to determine if the script is already injected or not. If it hasn't been injected yet, I want to inject the content script. This condition m ...

Utilizing a button's "data-" attribute to trigger a specific JavaScript function

I am trying to assign data to my buttons in a way that makes it accessible when clicked. While I can easily use JSON in a button's data attribute with a string as the key value, I am struggling to set the values to be a function instead. What I want ...

What steps should I take to verify that the most recent version of a static website is being shown?

As a user, I often find myself hitting 'F5' or clearing my cache to ensure I'm seeing the most up-to-date content on dynamic websites. However, I'm curious if there are any techniques from a web designer's perspective that can aut ...