polling - Angularjs how to access scope data outside the angular-poller function -


i'm pooling data using ng-poller function , splice data/json file. problem if navigate between tabs/menu data being spliced each time. need able poll data , access them outside function not being spliced every-time navigate through site.

here's controller:

   'use strict';     angular         .module('myapp')         .controller('analyticshistoryctrl', ['$scope', 'poller', 'analyticshistory', function ($scope, poller, analyticshistory) {              var datatosplice;             var polleranalyticshistory;             polleranalyticshistory = poller.get (analyticshistory, {delay: 10000});             polleranalyticshistory.promise.then (null, null, function (data) {                   //this works fine splices                  //data every 10000ms not                 $scope.mydata = data.analytics.splice(0,5);                  //i'm trying access outside                $scope.mydata = data.analytics;                datatosplice = $scope.mydata;                });               //outside poller here want access data , splice them              //to pass them ng-grid              datatosplice.splice(0,5);//this not work              $scope.mydata.splice(0,5);//this doe not work either                $scope.gridoptions = {                 data: 'mydata',                 columndefs: 'columns'              }  }]); 

here's plunker: http://plnkr.co/edit/ui279rl9jzvxgujxlklb?p=preview

many help.

you can check out plunk - http://plnkr.co/edit/xkqm7na91jlmhcxat0qn?p=preview.

step 1 : setup event handler (splice handler) using $scope.$on  step 2 : call $emit polleranalyticshistory.promise.then notify event handler data arrives  step 3 : inside event handler splice data , update grid  step 4 : unbind  event handler each time $scope destroyed. 

edit: better , simpler approach (8/18/2014) :

step 1 : create blank scope model ($scope.mydata)  step 2 : add watch on $scope.mydata , when ever mydata changes, splice it.  step 3 :  update $scope.mydata inside polleranalyticshistory.promise.then 

plunk updated : http://plnkr.co/edit/xkqm7na91jlmhcxat0qn?p=preview.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -