Angularjs how to access scope data from outside

2019-03-03 17:25发布

问题:

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 for service:

'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 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?

many thanks for help

PLUNKER: http://plnkr.co/edit/ui279rL9JZvxgUJXlkLB?p=preview

回答1:

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.

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

//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

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

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

}]);    

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

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



回答2:

You can use $rootscope.broadcast in service and then listen for it in controller by $scope.on