angularjs - Promise with data attribute not working -


i have factory promise supposed poll data webservice @ interval of 5 seconds. data fetched controller , parsed. poller initiated app.run.

problem data seems non-accessible controller, why this?

(just looking @ start wonder if livedata var threadsafe)

   factory('livedatapoller', ['$http', '$timeout', function($http, $timeout) {         var livedata = {                 status: -1,                 events: [],                 checksum: 0,                 servertime: 0,                 calls: 0             };         var poller = function() {                 $http.get('/api/getinformation.json')                     .then(function(res) {                          status = res.statuscode;                          if(status < 0) {                             // todo: handle service error                         } else {                             livedata.events = res.events;                             livedata.checksum = res.checksum;                             livedata.servertime = res.servertime;                         }                          livedata.status = status;                          livedata.calls++;                          $timeout(poller, 5000);                     });             };          poller();          return {             getdata: function() {                 return livedata;             }         };     }]) 

the controller:

angular.module('myapp.controllers', [])     .controller('mainctrl', ['$rootscope', '$scope', '$timeout', 'livedatapoller', function($rootscope, $scope, $timeout, livedatapoller) {             var transformlivedata = function(livedata) {                 var livedataobj = {                         servertime: livedata.servertime,                         checksum: livedata.checksum,                         events: [],                         calls: livedata.calls                     },                     events = [],                     i;                  if(livedata.events) {                     for(i = 0; < livedata.events.length; i++) {                         events.push({                             id:                 livedata.events[i].id,                             name:               livedata.events[i].details[1],                             freetext:           livedata.events[i].details[2],                         });                     }                      livedataobj.events = events;                 }                  return livedataobj;             }          $rootscope.livedata = transformlivedata(livedatapoller.getdata());       }]) 

the problem line returning livedata in service executed when $http call in progress, wrap livedata object around promise , work promise in controller. or, poor mans approach, watch livedata object in controller:

$scope.$watch(livedatapoller.getdata,function(value){     console.log(value); },true) 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -