AngularJS:$ routeProvider当决心$ HTTP响应返回的obj不是我obj的 [英] AngularJS: $routeProvider when resolve $http returns response obj instead of my obj

查看:166
本文介绍了AngularJS:$ routeProvider当决心$ HTTP响应返回的obj不是我obj的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解决一对夫妇的Ajax调用,这样的数据我的控制器需要是之前使用(它提供以下的指令)执行。然而,执行的顺序是工作,而不是返回我创建对象,结果注入到我的控制器是$ HTTP的响应对象:

  {
  配置:{...},
  数据:{...},
  标题:{...},
  状态:200
}

我的code基本上是这样的:

 的app.config([
  '$ routeProvider',函数($ routeProvider)
  {
    $ routeProvider
      。当('/路径',{
        ...
        解决:{
          数据:
            $ HTTP,
            功能($ HTTP)
            {
              返回$ HTTP
                获得('/ API /数据)
                .success(功能(数据,状态){返回data.rows [0];})
                .error(功能(数据,状态){返回false;});
            }
          ]
        }
      });
  }
]);

是我疯狂地?不应该从返回值$ HTTP的成功实际上是什么$ HTTP?

返回

我也试过

  ...
解决:{
  数据:
    $ HTTP,
    功能($ HTTP)
    {
      VAR响应;
      $ HTTP
        获得('/ API /数据)
        .success(功能(数据,状态){响应= data.rows [0];})
        .error(功能(数据,状态){响应= FALSE;});
      返回响应;
    }
  ]
}

但随后对象注入到我的控制器,未定义的数据(我猜是因为$ HTTP是异步的,解析并没有被$ HTTP-所以返回之前的$ HTTP准备)。

P.S。的$ HTTP的同步性应该定义在它的选项对象!

解决方案

 的app.config([
  '$ routeProvider',函数($ routeProvider)
  {
    $ routeProvider
      。当('/路径',{
        ...
        解决:{
          数据:
            $ HTTP,
            功能($ HTTP)
            {
              返回$ HTTP
                获得('/ API /数据)
                。然后(
                  函数成功(响应){返回response.data.rows [0]; },
                  功能错误(原因){返回false; }
                );
            }
          ]
        }
      });
  }
]);

感谢阿贾伊beniwal的指针和<一个href=\"http://stackoverflow.com/questions/17706419/angularjs-routeprovider-when-resolve-http-returns-response-obj-instead-of-my#comment25803901_17706419\">Mark Rajcok的指针。

P.S。 则()是记录上的 $ q 的页面


解决方案

  

$ HTTP @Returns {} HttpPromise返回{@link NG。$ Q承诺}对象
  与
          标准然后方法和两个HTTP具体方法:成功错误。在然后
          方法有两个参数的成功和错误回调将与一个被称为
         响应对象。在成功错误方法需要一个参数 - 一个函数,
          当请求成功或失败,分别会调用。这些参数传递到
          这些功能的解构再传递到响应对象的presentation
          然后方法。响应对象具有以下属性:


I'm trying to resolve a couple ajax calls so that data my controller needs is available before it (and a directive it furnishes) execute. The order of execution is working, however, instead of returning the object I create, the result injected into my controller is $http's response object:

{
  config: { … },
  data: { … },
  headers: { … },
  status: 200
}

My code essentially looks like:

app.config([
  '$routeProvider', function($routeProvider)
  {
    $routeProvider
      .when('/path', {
        …,
        "resolve": {
          "data": [
            '$http',
            function($http)
            {
              return $http
                .get('/api/data')
                .success(function(data,status) { return data.rows[0]; })
                .error(function(data,status)   { return false; });
            }
          ]
        }
      });
  }
]);

Am I daft? Shouldn't the return value from $http's success actually be what is returned by $http?

I also tried

…
"resolve": {
  "data": [
    '$http',
    function($http)
    {
      var response;
      $http
        .get('/api/data')
        .success(function(data,status) { response = data.rows[0]; })
        .error(function(data,status)   { response = false; });
      return response;
    }
  ]
}

But then the data object injected into my controller was undefined (I'm guessing because $http is asynchronous and resolve was not blocked by $http—so it returned before $http was ready).

P.S. The synchronicity of $http should be definable in its options object!!

Solution

app.config([
  '$routeProvider', function($routeProvider)
  {
    $routeProvider
      .when('/path', {
        …,
        "resolve": {
          "data": [
            '$http',
            function($http)
            {
              return $http
                .get('/api/data')
                .then(
                  function success(response) { return response.data.rows[0]; },
                  function error(reason)     { return false; }
                );
            }
          ]
        }
      });
  }
]);

Thanks to Ajay beniwal's pointer and Mark Rajcok's pointer.

P.S. then() is documented on $q's page.

解决方案

$http @returns {HttpPromise} Returns a {@link ng.$q promise} object with the standard then method and two http specific methods: success and error. The then method takes two arguments a success and an error callback which will be called with a response object. The success and error methods take a single argument - a function that will be called when the request succeeds or fails respectively. The arguments passed into these functions are destructured representation of the response object passed into the then method. The response object has these properties:

这篇关于AngularJS:$ routeProvider当决心$ HTTP响应返回的obj不是我obj的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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