Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
139 views
in Technique[技术] by (71.8m points)

javascript - 在服务中处理$ http响应(Processing $http response in service)

I recently posted a detailed description of the issue I am facing here at SO.

(我最近公布的我面对这个问题的详细说明, 这里的SO。)

As I couldn't send an actual $http request, I used timeout to simulate asynchronous behavior.

(由于我无法发送实际的$http请求,因此我使用超时来模拟异步行为。)

Data binding from my model to view is working correct, with the help of @Gloopy

(在@Gloopy的帮助下,从我的模型到视图的数据绑定工作正确)

Now, when I use $http instead of $timeout (tested locally), I could see the asynchronous request was successful and data is filled with json response in my service.

(现在,当我使用$http而不是$timeout (在本地测试)时,我可以看到异步请求成功并且data在我的服务中填充了json响应。)

But, my view is not updating.

(但是,我的观点不是更新。)

updated Plunkr here

(在这里更新了Plunkr)

  ask by bsr translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

(这是一个可以满足您需求的Plunk: 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.

(这个想法是你直接使用promises和他们的“then”函数来操作和访问异步返回的响应。)

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 ):

(这是一个稍微复杂的版本,可以缓存请求,因此您只能第一次创建它( 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;
    });
  };
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...