Angularjs如何从外部访问的数据范围 [英] Angularjs how to access scope data from outside
问题描述
我有民调数据中的每10000ms这是很好的,但是我需要能够从外部访问的变量,这样我可以用这个轮询功能拼接
功能。
I have this poller function that polls data every 10000ms which is fine, however I need to be able to access variable from outside so that I can use splice
function.
code服务:
'use strict';
//service to get data for analytics page
angular
.module ('myApp')
.factory('Analyticshistory', function ($resource) {
return $resource('analytics_history.json',{}, {'query': {method: 'GET', isArray: false}});
});
code的控制器:
code for 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 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'
}
}]);
我在做什么错在这里?
what am I doing wrong here?
非常感谢帮助
PLUNKER: http://plnkr.co/edit/ui279rL9JZvxgUJXlkLB?p=preVIEW
推荐答案
看起来像poller.get是一个异步调用。所以你的时候调用dataToSplice.splice(0,5);数据可能不是present那里dataToSplice
looks like poller.get is an asynchronous call . So by the time you call dataToSplice.splice(0,5); data may not be present there in dataToSplice.
这就是为什么拼接不起作用外promise.then的原因()
Thats the reason why splice doesn't work outside promise.then()
//轮询这里外我想访问数据,并且它们拼接//对
它们传递到到NG-电网dataToSplice.splice(0,5); //这不
工作$ scope.myData.splice(0,5); //这母鹿也不行
//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
现在你有两个选择 -
Now you have two options -
-
继续与你正在做的是选择的方式1
continue with the way you are doing as option 1
或设置一个单独的事件处理程序(可以在同一个控制器或其他控制器完成),并调用$放出里面pollerAnalyticsHistory.promise.then
or setup a separate event handler (this can be done in same Controller or another controller) and call $emit inside pollerAnalyticsHistory.promise.then
看我的岗位上如何处理事件(通过$ $和发射)
<一href=\"http://stackoverflow.com/questions/25301581/angular-js-newbie-link-in-a-controller-view-that-triggers-another-controller-a/25305951#25305951\">Angular JS新手 - 在控制器视图链接触发另一个控制器动作
Look at my post on how to handle events (through $on and $emit) Angular Js newbie - link in a controller view that triggers another controller action
编辑:(这应该工作) -
Edit : (this should work) -
angular
.module('myApp')
.controller('analyticshistoryCtrl', ['$rootScope', '$scope', 'poller', 'Analyticshistory', function ($rootScope, $scope, poller, Analyticshistory) {
$rootScope.$on("SpliceHandler", function(evt, next, current) {
$scope.myData.splice(0,5);
}
pollerAnalyticsHistory.promise.then (null, null, function (data) {
//I'm trying this to access this outside
$scope.myData = data.analytics;
dataToSplice = $scope.myData;
$scope.$emit("SpliceHandler");
}
}]);
编辑(2014年8月16日):
看看这里: http://plnkr.co/edit/xkQM7NA91JlmHcxat0Qn?p=$p $ PVIEW
编辑(2014年8月18日):
忘了取消绑定监听器。普拉克更新。
Edit(8/18/2014) : forgot to unbind the listener. plunk updated.
这篇关于Angularjs如何从外部访问的数据范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!