Answer the question
In order to leave comments, you need to log in
How to update data on a socket?
Hello.
I'm not good at Angular, I need help.
To work with sockets, I use https://github.com/AngularClass/angular-websocket
There is a faqAnswer directive that specifies a template
templateUrl: '/app/faq/directives/faq-answer.html',
module('app')
.directive('faqAnswer', faqAnswer)
.factory('faqSocket', function($websocket,$rootScope) {
var dataStream = $websocket('wss://api.myserver.com/faq/');
dataStream.onMessage(function(message) {
$rootScope.$apply(function () {
$rootScope.answer = message['data'];
});
- the data is displayed, but I think it's somehow clumsy and wrong. Answer the question
In order to leave comments, you need to log in
Touching $rootScope is not very practical, as an option to do something like this
.factory('faqSocket', function($websocket,$rootScope) {
var factory = {}
var dataStream = $websocket('wss://api.myserver.com/faq/');
dataStream.onMessage(function(message) {
$rootScope.$apply(function () {
factory.answer = message['data'];
});
factory.dataStream = dataStream ;
return factory;
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question