Troubleshooting: AngularJS fails to refresh UI when using SignalR

When I attempt to incorporate this angularjs code into a signalR function, the UI does not update. Even using $scope.$apply() does not trigger a digest.

This is the code snippet:

var notificationHub = $.connection.progressNotificationHub;
notificationHub.client.displayAdminNotification = function (currentProgress, totalProgress, msg, finish) {
    $scope.$apply(function () {
        if ($scope.adminNotification == null)
            $scope.adminNotification = {};

        $scope.adminNotification.currentProgress = currentProgress;
        $scope.adminNotification.totalProgress = totalProgress;
        $scope.adminNotification.msg = msg;
        $scope.adminNotification.percentage = totalProgress == 0 ? 0 : (currentProgress / totalProgress) * 100;
        $scope.adminNotification.finish = finish;

        if (finish) {
            $scope.adminNotification = null;
        }
    })
};

$.connection.hub.start().done(function () {
});

Here is my UI markup:

<!-- Notification -->
<div class="admin-notification-container" ng-show="adminNotification != null && showAdminNotification">
    <div class="dark-overlay" style="opacity:0.9"></div>
    <div class="admin-notification-content">
        <div class="admin-notification-progress">
            <p>Progress: {{adminNotification.currentProgress}} of {{adminNotification.totalProgress}}</p>
            <div class="admin-notification-progress-bar-container">
                <div class="admin-notification-progress-bar progress-bar progress-bar-striped progress-bar-animated" style="width:{{adminNotification.percentage}}%">{{adminNotification.percentage}}%</div>
            </div>
            <p class="admin-notification-msg">{{adminNotification.msg}}</p>
        </div>
    </div>
    <div class="hide-admin-notification" ng-click="toggleAdminNotification()">-</div>
</div>

<div ng-show="adminNotification != null && !showAdminNotification" class="hide-admin-notification-hide" ng-click="toggleAdminNotification()">^</div>

Update

Upon further investigation, it appears that when the webapi server sends a message to the UI, the '$scope' used within the displayAdminNotification function is different from the declared $scope. It creates a new $scope instance and uses that instead.

What could be causing this discrepancy?

Answer №1

It seems that my controller is being triggered twice due to nested controllers in my setup.

<div ng-app="app" ng-controller="ctrl">
    ......
        <div ng-app="anotherApp" ng-controller="controller"></div>
</div>

In addition, I am using a controller that extends another one, like so:

app.controller('ctrl', adminController);

adminController.$inject = ["$scope", "$controller", "$http", '$sce'];

function adminController ($scope, $controller, $http, $sce)
{
    angular.extend(this, $controller('controller', { $scope: $scope }));
    .....

If you are facing a similar issue, it's advisable to review the structure of your controllers.

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

What sets apart computed, state, and action in MobX?

Currently, I am delving into MobX and grappling with the concepts of computed, state, and action individually. Do they serve similar purposes? One aspect of MobX that intrigues me is deciphering the disparity between computed properties and state. My unde ...

Using ReactJS and JavaScript to transform an array into a fresh array

I am working with an array that looks like this: var oldArray = [ {number: '12345', alphabet: 'abcde'}, {number: '54321', alphabet: 'abcde'}, {number: '67891', alphabet: 'abcde'}, ...

Exploring the world with an interactive map in R, incorporating Shiny and leaflet

I'm currently attempting to integrate a Google layer as the base layer for a Leaflet map in Shiny R. To achieve this, I've been utilizing shinyJs to inject a JavaScript script into my R code and add the map. However, I'm facing an issue wher ...

Issues arise when attempting to delete messages that have already been retrieved

Having trouble removing messages from a specific user without any success: bot.js client.on("message", (message) => { if (message.content === '$deleteuser') { message.channel.fetchMessages({limit: 10}).then(collec ...

What is the best way to implement a scroll-into-view feature for a newly added list item in Vue.js?

I am currently utilizing vueJS to create a task viewing application. My goal is to make the div containing the list focus on the newly added list item immediately after adding a new task. Here's the HTML code from my template for the task list: < ...

How to toggle a class using ng-click in AngularJS

Currently, I am working on a project using AngularJS for my website. One specific feature involves having a form that is initially hidden with "display:none" set deliberately. There's also a button labeled create. My goal is to have the form toggle b ...

Encountering issues with scope: Unable to retrieve value and receiving an error message stating 'Cannot assign value to undefined property'

var mainApp = angular.module("Main", []); mainApp.controller("CtrlMain", [ function ($scope) { $scope.amount = 545 }]);` var app = angular.module("Main", []); app.controller("MainCtrl", [ function ($scope) { $scope.value = 545 ...

Error message: Unauthorized request error with the change.org JavaScript API

I am currently working on integrating the javascript API from change.org in order to retrieve all of my signed petitions for display on my source forge page. However, I am encountering an unauthorized request response from the change.org API. Despite tryi ...

Dynamically loading a webpage with an element roster

I have a list and I want it so that when a user clicks on the sport1 list item, results from the database table sport1 will be displayed. Similarly, if they click on the education1 list item, results from the education1 table should be shown. The issue i ...

How can I call a function from one Vue component in another component?

I have developed a component with the function "logout" as seen in the code snippet below: // @/component/Painel.vue <template></template> <script> export default { name: 'panel', methods: { logout: function () { ...

Is it possible to show an image without altering the Box dimensions?

Hi there, I am currently working on designing a footer and I have encountered an issue. I want to add an image in a specific position, but when I do so, it affects the size of the box. I was wondering if there is a way to set the image as a background or b ...

Server side processes automatically converting boolean parameters in Axios get requests to strings

My code involves a JSON object being passed as parameters to the Axios GET API. Here is the JSON object: obj = { name: "device" value: true, } The Axios GET request is made with the above object like this - tableFilter = (obj) => { ...

Unable to access the following element using jQuery's next() method

Can someone help me understand how the "next" function works in jQuery? I'm trying to reveal a hidden textarea when a span element is clicked. The hidden textarea is supposed to be the immediate next sibling of the span, but it's not working as e ...

Change the color of the navbar when scrolling using bootstrap and jquery

Using Bootstrap to design a navigation bar, I have two main goals: I want the navbar to change color when the page is scrolled down by 20%, and then revert back to its original color when scrolling back to the top. When the collapse featu ...

Is the branch of ExtJS 4.1 TreeStore lazy loading extending?

I am working on implementing lazy loading of tree branches in an MVC application using extjs4.1. The branches are located on different URLs and I have faced several challenges along the way. Unfortunately, at this point, the branching functionality is not ...

What are the steps to integrate MaterializeCss into Vue.js?

I prefer not to utilize Vue-Material or Vuetify. My choice is Materialize. Here's what I do: npm install materialize-css@next In my main.js file, where I define my new Vue App, I import Materialize like this: import 'materialize-css' Th ...

Creating a custom jQuery selector

I've been struggling with a particular problem all day today, trying different approaches but still unable to find a solution. The crux of the issue is this: I have multiple JavaScript functions running to determine whether certain variables should b ...

Angular Material Popup - Interactive Map from AGM

I am in the process of developing a material dialog to collect user location information using AGM (angular google maps). I have successfully implemented a map on my main page, but when the dialog is opened, it only shows a blank space instead of the map. ...

What is the best way to delete all tab items except for one in a wpf application?

One issue I'm facing is that when I click the logout button, I want to remove all tab items. Here's my current code: foreach (object item in mainTab.Items) { TabItem ti = (TabItem)item; if ("welcomeTabItem" != ti.Name) { mainTab.I ...

Iterate over rows in a b-table component in Vue

In the context of Vue bootstrap, a b-table consists of a list with an unknown number of rows. The requirement is to display a remove button only if the email in the row matches that of the currently logged-in user. This functionality currently works for a ...