$rootScope:task_in_prog The action is currently underway and cannot be started again

Working with Angular Material has posed challenges for me, particularly in grasping the intricacies of the $scope.apply functionality. Whenever I attempt to notify the UI of changes by invoking apply, it consistently fails with an error. While similar questions exist on SO, my unique situation involves data not being stored by the scope but rather by the controller instance itself.

It's worth noting that I've opted to utilize TypeScript for my controller implementation, which seems somewhat unconventional given that most sample code I've come across features inline controller implementations directly within the view.

This is a snippet of what my controller looks like...

module MyApp {

    export class MyController {

        private scope: angular.IScope;
        private http: angular.IHttpService;

        public model: { data: SampleData } = { data: { } };

        constructor($scope: angular.IScope, $http: angular.IHttpService) {
            this.scope = $scope;
            this.http = $http;
        }

        public querySampleData(): void {

            const serviceEndpoint: string = "http://localhost:8080/api/";
            var promise: ng.IHttpPromise<SampleData> = this.http.get(serviceEndpoint + "sample", { });

            var callback: ng.IHttpPromiseCallback<SampleData> = (
                data: SampleData,
                status: number,
                headers: angular.IHttpHeaderGetter,
                config: angular.IRequestConfig) => {
                this.scope.apply(() => {
                    this.model.data = data;
                };
            };

            promise.success(callback);
        }
    }

    export interface SampleData {
        message? string;
    }
}

The controller showcases the querySampleData method, responsible for fetching data from a remote service using IHttpService for asynchronous API calls. The way the controller is utilized in the view is as follows...

<html ng-app="app">
    <body ng-controller="MyController as c">
        <div>
            <button ng-click="c.querySampleData()" 
                    aria-label="Query sample data">Query sample data</button>
            <p>{{c.model.data.message}}</p>
        </div>
        ...
        <script src="../scripts/app.js"></script>
        <script type="text/javascript">
            var app = angular.module("app", ["ngMaterial"]);
            app.controller("MyController", [ "$scope", "$http", MyApp.MyController ]);
        </script>
    </body>
</html> 

The initialization of the controller proceeds smoothly; all dependencies are appropriately injected into the controller, and the binding to the querySampleData method operates as anticipated (triggered when the button is clicked). Despite assigning the retrieved data to the model, the UI does not update accordingly. As a workaround, I included a call to the $apply method to inform the UI of changes, only to encounter the aforementioned error.

Update:

Referencing the documentation at: https://docs.angularjs.org/error/$rootScope/inprog suggests replacing $apply with $timeout for ensuring asynchronous execution. Although this resolves the error, it unfortunately doesn't reflect in UI updates...

Answer №1

When working with AngularJS promises, keep in mind that the callbacks already execute .apply() internally. Trying to call .apply() again within the callbacks will result in an error message saying "In Progress". Simply remove the unnecessary .apply() and everything should function as expected, unless there are additional issues present.

Answer №2

Why not give it a shot?

Try setting this.model.data to the data parameter
and afterwards invoke this.scope.apply();

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

The useEffect hook is failing to resolve a promise

I have received a response from an API that I need to display. Here is a snippet of the sample response (relevant fields only): [ { ...other fields, "latitude": "33.5682166", "longitude": "73 ...

What is the best method for swapping out an iframe with a div using Javascript?

Having an issue with loading an external HTML page into an iFrame on my website. Currently facing two main problems: The height of the iFrame is fixed, but I need it to adjust based on the content's height. The content inside the iFrame does not inh ...

What is the best way to transfer a variable from a Node.js Express server to an EJS HTML file in order to toggle alert visibility?

Hello, I am currently facing a challenge in sending a variable from my app.js file to my ejs HTML file in order to toggle the display of an alert. Here is what the relevant part of my app.js code looks like: view image description here Initially, I attem ...

What is the method for triggering a browser notification at a designated time?

I am working on a to-do app using the MEAN stack. Users will input their to-do tasks along with the time they need to be completed, which will then be stored in a database. My current code retrieves the client's browser time and sends a notification b ...

When aot is enabled, ngClass and ngIf condition may not compile successfully

I am encountering an issue with a div using ngClass and ngIf conditions: <div [ngClass]="{ 'active': nbActive === 1 }" > <!-- some stuff --> </div> There is also a similar div using a ngIf condition: <div *ngIf="nbActi ...

Issue with modal not being able to close the embedded video iframe

I have created a demo of a video in my footer that uses external CSS files and works perfectly. However, when I added it to the project, everything works except for closing the video. After clicking the play button in the footer, a modal appears on the sc ...

Utilizing React and Redux selectors for filtering and sorting in applications

I am looking to implement a feature that allows users to sort and filter a list of items based on attributes. I believe using selectors would be the most efficient way to achieve this, but I need some guidance on how to do it exactly. My idea is to have ...

A guide to implementing Quasar Framework and/or Vue3 using Bun.js

After using the bun create [..] command to easily create a react and a next project by following the instructions on the bun git repository (note: additional instructions are available at bun.sh), I encountered a problem with setting up quasar/vue. While ...

v-show is not functioning properly

After clicking the button, I notice that even though the array shows[] changes (as indicated by Vue's chrome plugin), the character 'a' remains on the page. However, characters 'b' and 'c' do not appear at all. ...

Utilizing Angular features such as Promises, iterating through a textarea using forEach, and ensuring the processing

As I venture into the realm of Angular, my programming background gives me confidence. It's like teaching an old dog new tricks :) The user inputs a list of dictionary words into a textarea for lookup. This triggers 3 API calls via $http get, combini ...

Using PHP to validate a read-only textbox

I am trying to implement validation on a Datepicker that is set to readonly, but I am facing issues with my current code. Can someone please assist me? Below is the JavaScript code used for error trapping: <script type="text/javascript"> $(func ...

Unlocking the intricacies of navigating through nested arrays of objects in JavaScript

I need help figuring out how to loop through a nested array of objects in my code. I've tried different approaches but can't seem to grasp how it works. The object data I have looks like this: [ { "restaurantName":"Bron ...

There seems to be an issue when trying to retrieve data from a JSON array stored

I have a SQL table with a column that stores JSON arrays. I am able to retrieve the data using Node.js, but when I try to manipulate the JSON array, I encounter errors consistently. Here is an example of my JSON array in the SQL database: { characters: [{ ...

Replace the indexOf() method to be called on a null value

I have discovered a way to override functions in JavaScript, such as indexOf(), like this: var indexOf = String.prototype.indexOf; String.prototype.indexOf = function(){ //MY CUSTOM CODE HERE return indexOf.call(this, arguments); }; By doing this ...

What is the purpose of using an img tag to retrieve a URL that is not an image?

Upon investigating a slow page load, I came across something interesting in the HTML: <img src="/ajax?action=askedAboutCookies" style="display:none" width="0" height="0" alt="" /> When the page loads, it sends a request but never receives a respons ...

Is the Quirks Mode on the Keycloak login-status-iframe.html page causing an issue with the home page being stuck?

SSO implementation in Angular application using Keycloak-js was successful with a local Keycloak server, but issues arose when switching to a remote Keycloak server in January 2022 after the Firefox update. Both Firefox and Chrome rejected cookies due to t ...

Ways to troubleshoot and resolve the jQuery error with the message "TypeError: 'click' called"

I am currently developing a project for managing Minecraft servers, focusing on a configuration panel. I have set up a form that users need to fill out in order to configure the settings and send the values using Ajax. However, I encountered an error: Type ...

WebSocket connection issues are being experienced by certain users

While using socket.io, I encountered an issue where some users were unable to send messages with the message "I can't send a message why?". After researching the problem, it seems that the firewall or antivirus software may be blocking websockets. If ...

Error: The function passport.use is undefined

I've been trying to integrate the passport service into my app, but I keep encountering this error. Despite installing all the required dependencies, I can't seem to resolve it. I hope someone here can offer some guidance: Here is the error mess ...

Distinctive titles for JavaScript constructors/prototypes compared to classes

When working with JavaScript ES6, classes allow us to write code like this: class RectangularShape { constructor(height, width) { this.height = height; this.width = width; } getArea() { return this.height * this.width } static some ...