M
M
mr_drinkens892017-01-13 19:59:57
Angular
mr_drinkens89, 2017-01-13 19:59:57

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',

Data is sent via sockets, and I display them in the console in the dataStream.onMessage method (as in the example in the lib above)
How can I update (binding) the data that came via the socket in the template?
Tried it this way:
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.
Thank you in advance for your help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SergeyBugai, 2017-01-13
@SergeyBugai

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;
})

In the directive itself, either output data directly from the factory or put a variable on the factory.answer variable

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question