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?