各种人使用 $ http 的方式不同。说样品1
$http({
method: 'GET',
url: 'api/book/',
cache: $templateCache
}).
success(function (data, status, headers, config) {
$scope.books = data;
}).
error(function (data, status) {
console.log("Request Failed");
});
这里成功和错误回调是通知用户。再次,很少有人使用$ http不同的方式,如
this.getMovie = function(movie) {
return $http.get('/api/v1/movies/' + movie)
.then(
function (response) {
return {
title: response.data.title,
cost: response.data.price
});
},
function (httpError) {
// translate the error
throw httpError.status + " : " +
httpError.data;
});
};
这里然后正在使用.......它是承诺样品吗?为什么人们会那么而不是成功? 那么的优势是什么?
承诺的意义和承诺的作用是什么? 何时使用角度承诺?
答案 0 :(得分:0)
每个角DOCS,看起来像1.4.4是第一个发出通知的版本(见下文)。
弃用通知
$ http遗留承诺方法成功与错误 弃用。请改用标准方法。如果 $ httpProvider.useLegacyPromiseExtensions设置为false然后这些 方法将抛出$ http / legacy错误。
从文档中,新的首选角度方式来执行所有$ http请求。(来自angular docs的代码)
General usage
The $http service is a
function which takes a single argument— a configuration object— that is used to generate an HTTP request and returns a promise.
// Simple GET request example :
$http.get('/someUrl').
then(function(response) {
// this callback will be called asynchronously
// when the response is available
}, function(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
// Simple POST request example (passing data) :
$http.post('/someUrl', {
msg: 'hello word!'
}).
then(function(response) {
// this callback will be called asynchronously
// when the response is available
}, function(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});