angular-websocket icon indicating copy to clipboard operation
angular-websocket copied to clipboard

Inside onClose callback scope changes never are applied

Open Serginho opened this issue 9 years ago • 3 comments

This code doesn't work:

ws.onClose(function (e) {
 $scope.anyVariable = anyValue;
}

I mean, scope variables are changed but not binding to the view. Binding is off

I fixed it using $scope.$digest(), but why not update?

Serginho avatar Mar 16 '15 13:03 Serginho

Same here, didn't thought of it first and wasted nearly an hour. Surrounding all on...() callbacks with $apply would be a good idea!

hohl avatar Aug 01 '15 08:08 hohl

Same here, I $broadcast an event to let the controller know, and the angular-websocket callbacks to onClose/onError are not made within an $apply block, so need to be wrapped:

ws.onError(function(event) {
    $rootScope.$apply(function() {
      $rootScope.$broadcast("wschange", 0);
    });
  });

  ws.onClose(function(event) {
    $rootScope.$apply(function() {
      $rootScope.$broadcast("wschange", 0);
    });
  });

shuckc avatar Nov 19 '15 12:11 shuckc

Same here, I think this should be documented or changed. Having onMessage update scope vars but not onClose was confusing until I found this issue.

daniel-nichter avatar Nov 28 '15 05:11 daniel-nichter