Processing $http response in service

后端 未结 12 1672
半阙折子戏
半阙折子戏 2020-11-22 03:36

I recently posted a detailed description of the issue I am facing here at SO. As I couldn\'t send an actual $http request, I used timeout to simulate asynchrono

12条回答
  •  情深已故
    2020-11-22 04:34

    Here is a Plunk that does what you want: http://plnkr.co/edit/TTlbSv?p=preview

    The idea is that you work with promises directly and their "then" functions to manipulate and access the asynchronously returned responses.

    app.factory('myService', function($http) {
      var myService = {
        async: function() {
          // $http returns a promise, which has a then function, which also returns a promise
          var promise = $http.get('test.json').then(function (response) {
            // The then function here is an opportunity to modify the response
            console.log(response);
            // The return value gets picked up by the then in the controller.
            return response.data;
          });
          // Return the promise to the controller
          return promise;
        }
      };
      return myService;
    });
    
    app.controller('MainCtrl', function( myService,$scope) {
      // Call the async method and then do stuff with what is returned inside our own then function
      myService.async().then(function(d) {
        $scope.data = d;
      });
    });
    

    Here is a slightly more complicated version that caches the request so you only make it first time (http://plnkr.co/edit/2yH1F4IMZlMS8QsV9rHv?p=preview):

    app.factory('myService', function($http) {
      var promise;
      var myService = {
        async: function() {
          if ( !promise ) {
            // $http returns a promise, which has a then function, which also returns a promise
            promise = $http.get('test.json').then(function (response) {
              // The then function here is an opportunity to modify the response
              console.log(response);
              // The return value gets picked up by the then in the controller.
              return response.data;
            });
          }
          // Return the promise to the controller
          return promise;
        }
      };
      return myService;
    });
    
    app.controller('MainCtrl', function( myService,$scope) {
      $scope.clearData = function() {
        $scope.data = {};
      };
      $scope.getData = function() {
        // Call the async method and then do stuff with what is returned inside our own then function
        myService.async().then(function(d) {
          $scope.data = d;
        });
      };
    });
    

提交回复
热议问题