我从元数据构造元素,我需要为每个元素设置一个计算的类。
这是我目前正在做的
var promisses =_.map(templates, function (tmpl) {
return $http.get(tmpl.template, {
cache : $templateCache,
// Generated class name is carried to the resolving function using the config
classes : scope.generate_class(tmpl.columns)
}).then(function (data) {
if ( data.status != 200 )
throw new Error('Failed to fetch template');
var elm = angular.element(data.data);
elm.addClass(data.config.classes);
return elm;
});
});
$q.all(promisses).success....
如果我想用fir位success
代替它(它也会在出现错误的情况下进行评估),我该怎么做?使用config时,不会继续使用解析功能(仅数据)。then
$http
success
谢谢。
返回带有标准
then
方法和两个http特定方法的promise对象:success
和error
。then方法接受两个参数成功和错误回调,将使用响应对象调用该回调。成功和错误方法采用单个参数-该函数将在请求成功或失败时分别调用。传递给这些函数的参数是传递给then方法的响应对象的结构化表示。响应对象具有以下属性:
- data – {string | Object} –使用转换函数转换的响应主体。
- status – {number} –响应的HTTP状态代码。
- 标头– {function([headerName])} –标头获取函数。
- config – {Object} –用于生成请求的配置对象。
- statusText – {string} –响应的HTTP状态文本。
因此,您可以像这样传递配置:
.success(function(data, status, headers, config) {
使用Promise时不要抛出错误,如果您的服务器未返回错误代码,则可以将q.reject
其转换为拒绝代码,q.all
Promise也没有成功方法:
var promisses =_.map(templates, function (tmpl) {
return $http.get(tmpl.template, {
cache : $templateCache,
// Generated class name is carried to the resolving function using the config
classes : scope.generate_class(tmpl.columns)
}).then(function(res) {
if ( res.status != 200 ) {
return $q.reject('Failed to fetch template');
} else {
var elm = angular.element(res.data);
elm.addClass(res.config.classes);
return elm;
}
});
});
$q.all(promisses)
.then(function() { ... })
.catch(function() { .. })
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句