0
$scope.add = function(){

      $scope.playerCards.push(Deck.drawCard());

      if (playerScore > 21){
        console.log("You Busted!");
        newHand();
      }

 };

In this blackjack game, the DOM automatically updates to reflect the players hand using ngRepeat. However, there isn't a window of time to allow the last card to show before the >21 logic executes. I'm guessing angular doesn't have time to run through ng-repeat. How can I force it to update as soon as the data updates? I tried $scope.$digest() but it throws an error about about something else being in progress.

Bernhard Hofmann
  • 10,321
  • 12
  • 59
  • 78
user2483724
  • 2,089
  • 5
  • 27
  • 43
  • You could take the `newHand()` call out of there. Maybe that should be called when the user clicks a button or something. Otherwise they're not going to be able to see the last card anyways. Also, check out this [`$scope.safeApply`](https://coderwall.com/p/ngisma) function that was created precisely for the purpose of applying changes when it is unknown if a digest is already in progress. – jshanley May 01 '14 at 16:23
  • Have a look at http://stackoverflow.com/questions/15207788/calling-a-function-when-ng-repeat-has-finished – joakimdahlstrom May 01 '14 at 16:25

1 Answers1

1

I'd solve that by delaying the test for going over 21, akin to the human delay of adding the cards and seeing they're bust. Maybe even give them enough time to see it for themselves before you tell them. :)

Bernhard Hofmann
  • 10,321
  • 12
  • 59
  • 78
  • I actually tried using $timeout and it works for displaying the card, but it also affects some other logic. I can probably make it work, but it seems useful to know if I can force angular to rerun ng-repeat. (Unless of course that's an antipattern) – user2483724 May 01 '14 at 16:24
  • Actually, waiting for that DOM manipulation would probably freeze up the browser so this way makes sense. – user2483724 May 01 '14 at 17:09