我试图解决一对夫妇的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的指针和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:
相关推荐
最新文章