Coder Social home page Coder Social logo

Loose access to $scope about ng-websocket HOT 2 OPEN

wilk avatar wilk commented on September 17, 2024
Loose access to $scope

from ng-websocket.

Comments (2)

kumorig avatar kumorig commented on September 17, 2024

ng-websockets events doesn't trigger a angular $digest. (Which I really think it should, btw.). Anyway, all you have to do is trigger it yourself.

$scope.message = message;
$scope.apply(); 

This can potentially trigger an error if angular is already in a digesting state. One easy way to waif for the previous digest to complete is to use $timeout. (You have to inject it):

angular.module('bedappApp').controller('MainCtrl', function ($scope, $timeout, $websocket) {
    ...
    ws.$on('event', function (message) {
        $timeout(function(){
            $scope.message = message;
        });
    });

(Using $timeout without a delay-value will run a function immediately or as soon as the current digest finishes).

from ng-websocket.

jfranki avatar jfranki commented on September 17, 2024

I am new to AngularJS and this one was driving me nuts, I even looked everything there is to know about scopes and inheritance.

The solution of @kumorig works, but is missing the '$':

$scope.message = message;
$scope.$apply();

It really is a valid workaround, but it kind of "de-angularizes" the main purpose of websockets:

$apply() is used to execute an expression in angular from outside of the angular framework.

Is there another way of achieving this with ng-websocket or something that we are missing?

from ng-websocket.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.