如何在angular-poll函数之外访问作用域数据



我使用ng-poller函数池化数据,然后我拼接数据/json文件。问题是,如果你每次都在标签/菜单数据之间进行导航。所有我需要能够轮询数据,然后访问它们的功能之外,所以他们不被拼接每次你通过网站导航。

这是我的控制器:

   '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 but it splices 
                //data every 10000ms which is not good
                $scope.myData = data.analytics.splice(0,5);

               //I'm trying this to access this outside
               $scope.myData = data.analytics;
               dataToSplice = $scope.myData;
              });
             //outside the poller here I want to access data and splice them
             //to pass them into to ng-grid
             dataToSplice.splice(0,5);//this does not work
             $scope.myData.splice(0,5);//this doe not work either

             $scope.gridOptions = {
                data: 'myData',
                columnDefs: 'columns'
             }
 }]);

这是Plunker: http://plnkr.co/edit/ui279rL9JZvxgUJXlkLB?p=preview

非常感谢您的帮助

您可以查看此链接- http://plnkr.co/edit/xkQM7NA91JlmHcxat0Qn?p=preview。

Step 1 : setup an event handler (splice handler) using $scope.$on
Step 2 : call $emit from pollerAnalyticsHistory.promise.then to notify the event handler that data arrives
Step 3 : inside the event handler splice you data and update the grid
Step 4 : unbind the  event handler each time the $scope is destroyed.
编辑:一个更好更简单的方法(2014年8月18日):
Step 1 : create a blank scope model ($scope.myData)
Step 2 : add watch on $scope.myData and when ever myData changes, splice it.
Step 3 :  update $scope.myData inside pollerAnalyticsHistory.promise.then

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

相关内容

  • 没有找到相关文章

最新更新