Angularjs如何访问AngularPoller函数之外的范围数据
我使用ng poller函数汇集数据,然后拼接数据/json文件。问题在于,如果您在每次拼接的选项卡/菜单数据之间导航。我所需要的是能够轮询数据,然后在函数外部访问它们,这样它们就不会在每次浏览站点时被拼接 这是我的控制器:Angularjs如何访问AngularPoller函数之外的范围数据,angularjs,polling,Angularjs,Polling,我使用ng poller函数汇集数据,然后拼接数据/json文件。问题在于,如果您在每次拼接的选项卡/菜单数据之间导航。我所需要的是能够轮询数据,然后在函数外部访问它们,这样它们就不会在每次浏览站点时被拼接 这是我的控制器: 'use strict'; angular .module('myApp') .controller('analyticshistoryCtrl', ['$scope', 'poller', 'Analyticshistory'
'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'
}
}]);
这是普朗克:
非常感谢您的帮助。您可以查看此plunk- 编辑:更好更简单的方法(2014年8月18日):
plunk updated:。无法查看使用新解决方案的更新版本。您现在可以查看它。请刷新plunk。好的,这似乎是一个更简单、更整洁的解决方案。哪一个更好?再次感谢你的帮助,你太棒了!请原谅我的傲慢,但我有一个问题看这里:你有没有可能解决这个问题?非常感谢:)在第一个解决方案中,事件会冒泡到rootsScope(要处理它,您应该在句柄捕捉到事件后取消它)。但第二个解决方案更整洁。不要冒泡。不知道为什么一开始我没有想到这个主意;)
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.
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