2

I am trying to get Angular JS working with web sockets, and after getting a basic set up from Angular web socket seed on Github, I am still facing a little problem with two way data binding. I am using a file like a config file, to set initial app values, which contains this:

  socket.emit('button:state', {
    state: 'default'
  });

Then I have a controller that deals with the state of the button initially, and then attempts to rewrite the initial state via the web socket:

function MyCtrl1($scope, socket) {

    // UI INITIAL STATE
    socket.on('button:state', function (data) {
        $scope.buttonState = data.state;
    });

    // UI CLICKED STATE
    $scope.buttonClicked = function(data) {
        //$scope.buttonState = 'clicked';
        socket.emit('button:state', {
            state: 'clicked'
        });
        console.log('button clicked');
    };
}
MyCtrl1.$inject = ['$scope', 'socket'];

The view is simply set up in Jade:

div(class='button', ng-class='buttonState', ng-click='buttonClicked()')

Can anyone shed some light, please?

1
  • Nothing really obvious here. I often find that setting a default value before any other of something like $scope.buttonState = '' or is useful to initialize and also keep track of variables that I use for state. Can you post the output of the Jade? Commented Jun 7, 2013 at 18:15

1 Answer 1

2

You're not in Angular's RunLoop. Try to add $scope.$apply(); after changing buttonState when you receive from the socket.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.