Angularjs 如何从外部访问范围数据 [英] Angularjs how to access scope data from outside

查看:20
本文介绍了Angularjs 如何从外部访问范围数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个轮询器函数,它每 10000 毫秒轮询一次数据,这很好,但是我需要能够从外部访问变量,以便我可以使用 splice 函数.

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.

服务代码:

'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}});
      });

控制器代码:

   '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

推荐答案

看起来 poller.get 是一个异步调用.所以当你调用 dataToSplice.splice(0,5);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.

这就是 splice 在 promise.then() 之外不起作用的原因

Thats the reason why splice doesn't work outside promise.then()

//这里的轮询器外我想访问数据并将它们拼接//到将它们传递给 ng-grid dataToSplice.splice(0,5);//这不会work $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

现在你有两个选择 -

  1. 继续你的方式作为选项 1

  1. continue with the way you are doing as option 1

或者设置一个单独的事件处理程序(这可以在同一个控制器或另一个控制器中完成)并在 pollerAnalyticsHistory.promise.then 中调用 $emit

or setup a separate event handler (this can be done in same Controller or another controller) and call $emit inside pollerAnalyticsHistory.promise.then

看我关于如何处理事件的帖子(通过 $on 和 $emit)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

(这应该有效)-

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");
}

}]);    

编辑(8/16/2014):在这里查看:http://plnkr.co/edit/xkQM7NA91JlmHcxat0Qn?p=preview

Edit(8/16/2014) : Check out here : http://plnkr.co/edit/xkQM7NA91JlmHcxat0Qn?p=preview

编辑(8/18/2014):忘记解除绑定监听器.plunk 更新了.

Edit(8/18/2014) : forgot to unbind the listener. plunk updated.

这篇关于Angularjs 如何从外部访问范围数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆