在业务处理$ http响应 [英] Processing $http response in service

查看:120
本文介绍了在业务处理$ http响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发表我面对这个问题<一的详细说明href=\"http://stackoverflow.com/questions/12504747/angularjs-processing-asynchronous-data-in-service\">here在SO。正如我无法发送实际 $ HTTP 要求,我用超时来模拟异步行为。从我的模型,查看正在正确的,与@Gloopy的帮助

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 asynchronous behavior. Data binding from my model to view is working correct, with the help of @Gloopy

现在,当我使用 $ HTTP 而不是 $超时(本地测试),我可以看到异步请求成功和数据充满了我的服务JSON响应。但是,我的看法没有更新。

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. But, my view is not updating.

更新Plunkr 这里

推荐答案

下面是一个普拉克说你想要做什么:的 http://plnkr.co/edit/TTlbSv?p=$p$pview

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

下面是缓存的要求,所以你只能使第一次(<一个稍微复杂一点的版本href=\"http://plnkr.co/edit/2yH1F4IMZlMS8QsV9rHv?p=$p$pview\">http://plnkr.co/edit/2yH1F4IMZlMS8QsV9rHv?p=$p$pview):

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

这篇关于在业务处理$ http响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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