angularjs - Passing scope data into $http function -
i'm constructing elements metadata , need set calculated class each element.
this do,
var promisses =_.map(templates, function (tmpl) { homecoming $http.get(tmpl.template, { cache : $templatecache, // generated class name carried resolving function using config classes : scope.generate_class(tmpl.columns) }).then(function (data) { if ( data.status != 200 ) throw new error('failed fetch template'); var elm = angular.element(data.data); elm.addclass(data.config.classes); homecoming elm; }); }); $q.all(promisses).success.... if want utilize success instead of then fir $http bit (which evaluates in case of error well) how ? when using success config not carried on resolving function (only data).
thanks.
from $http docs:
returns promise object standard then method , 2 http specific methods: success , error. method takes 2 arguments success , error callback called response object. success , error methods take single argument - function called when request succeeds or fails respectively. the arguments passed these functions destructured representation of response object passed method.
the response object has these properties:
data – {string|object} – response body transformed transform functions. status – {number} – http status code of response. headers – {function([headername])} – header getter function. config – {object} – configuration object used generate request. statustext – {string} – http status text of response.so can pass config so:
.success(function(data, status, headers, config) { do not throw errors when using promises, if server doesn't homecoming error code can utilize q.reject transform rejection, q.all promises doesn't have success method:
var promisses =_.map(templates, function (tmpl) { homecoming $http.get(tmpl.template, { cache : $templatecache, // generated class name carried resolving function using config classes : scope.generate_class(tmpl.columns) }).then(function(res) { if ( res.status != 200 ) { homecoming $q.reject('failed fetch template'); } else { var elm = angular.element(res.data); elm.addclass(res.config.classes); homecoming elm; } }); }); $q.all(promisses) .then(function() { ... }) .catch(function() { .. }) angularjs promise
No comments:
Post a Comment