AngularJS installations are delayed when the code is inside the SignalR callback

I have some code that updates AngularJS $ area properties. If this code is run in a function, the web page is updated immediately. However, the same code when working inside the SignalR callback function will not immediately refresh the web page.

Code that immediately binds bindings:

HTML with Angular bindings:

<input id="name" type="text" ng-model="playerName" /><button ng-click="RegisterPlayer();">Register</button>

Function inside Angular controller:

  $scope.RegisterPlayer = function () {

      $scope.numberOfPlayers += 1;
      var player = {
          name: $scope.playerName,
          randomNumber: 0,
          totalScore: 0
      }
      $scope.players.push(player);
  }

When this is done, the number of players immediately opens on the web page.

This is how I reorganized the code to send information to the SignalR hub, and then used the same code in the SignalR callback:

These are the same functions as above, but now it just sends information to the SignalR hub:

  $scope.RegisterPlayer = function () {

      hub.server.send($scope.playerName, -1);
  }

( , ):

  function receiveSignalRMessage(name, message) {
      var randomNumber = parseInt(message, 10);

      // -1 means the player was registered
      if (randomNumber == -1) {
          $scope.numberOfPlayers += 1;
          var player = {
              name: $scope.playerName,
              randomNumber: 0,
              totalScore: 0
          }
          $scope.players.push(player);
          return;
      }
  }

, , - . , , . ?

+4
1

Angular , "" .

angular, , , $scope.apply:

: http://jimhoskins.com/2012/12/17/angularjs-and-apply.html

+3

Source: https://habr.com/ru/post/1536119/


All Articles