AngularJS share asynchronous service data between controllers

断了今生、忘了曾经 提交于 2020-01-02 20:08:26

问题


I know this issue has been asked several times in stackoverflow, but I couldn't find a proper answer.

What I need to achieve is to be able to initiate some asynchronous call in one controller and when the result returns update the $scope in some other controller.

I understand that I should use a shared service that actually does the $http stuff, but I can't manage to update the other controller scope.

Here is my code:

View

<div class="screens" ng-controller="framesController">
   <div class="scroller scroll-pane" frames-scroll-pane>
         <div class="overview"> 
              {{frames}}                      
         </div>
   </div>
</div>

Service

 angular.module('BrightspotApp.models', []).
    factory('framesModel', function($http,globals){
       var api_path = 'clickard/GetClickardById/'; 
       var sharedService = {};

       sharedService.getAsync = function (id) { 
            sharedService.frames = {};

            $http.get(globals.serverUrl + api_path + id).
                success(function (data, status, headers, config) {
                    sharedService.frames = data;

                }).error(function (data, status, headers, config) {
                    console.log('HTTP error');
                });

        };

        return sharedService;

      });

Controllers

angular.module('BrightspotApp.controllers', []).

/**
* Top menu controller
*/
controller('menuController', function($scope,framesModel){
    $scope.clicked = false;
    $scope.toggle = function (item) {       
        $scope.clicked = !$scope.clicked;
        if ($scope.clicked){
               framesModel.getAsync(1);
        }
    };

    $scope.itemClass = function(item) {
        return $scope.clicked ? 'active' : undefined;
    };
}).
/**
 * Frames controller
 */ 
controller('framesController', function($scope,framesModel){

    $scope.data = [];
    $scope.frames = framesModel;
    });

This thing kind of works in the sense that it actually updates the $scope and consequently the DOM when the async call returns.

But what I need to achieve is to get a callback when the async finishes (maybe with a $broadcast) to notify the frames controller and then to have control over the returned data so that I can manipulate it before updating the $scope and consequently the DOM.

Assistance will be much appreciated.


回答1:


Something like this:

$http.get(globals.serverUrl + api_path + id).then(function(result){
    sharedService.frames = result.data;
});

If you want your getAsync method to return the data when the call has completed then you can use defer:

sharedService.getAsync = function (id) { 
    sharedService.frames = {};
    var defer = $q.defer();
    $http.get(globals.serverUrl + api_path + id).
    then(function(result){
        sharedService.frames = result.data;
        defer.resolve(result.data);
    }).error(function (data, status, headers, config) {
        console.log('HTTP error');
    });
    return defer.promise;
};


来源:https://stackoverflow.com/questions/18125076/angularjs-share-asynchronous-service-data-between-controllers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!