Can*_*ern 5 angularjs angularjs-http angular-promise angularjs-q
首先,我不擅长angularjs.
虽然我一直在研究$ q,但我遇到了一个奇怪的问题.
当我使用$ q.all时,我将$ http放在常规序列中,期望得到相同顺序的结果,
但我得到的是随机结果.
看到这个并纠正我的愚蠢.
$q.all([
HttpService.editItem(
$scope.$parent.category_id, // category id
Define.CAR_CAT, // category url to request
$scope.car_id, // car_id wanna edit
{car_name: inputValue.toUpperCase()} // data
),
HttpService.getCarList(
$scope.$parent.category_id, // category id
Define.CAR_CAT // category url to request
)
]).then(function (results) {
if (results[0].statusText === 'OK' && results[1].statusText === 'OK') {
.....
});
Run Code Online (Sandbox Code Playgroud)
'HttpService'是我的应用程序的服务.它会回报承诺.
我的期望是什么
首先编辑汽车名称,稍后获取汽车清单.
但我得到的结果是先获得车名,然后再编辑车名.
我正在使用
return $ q(function(resolve,reject){});
而不是使用
$ q.defer();
.
.
.
.
这些是我的HttpService部分
function editItem(cat_id, cat_url, content_id, item_data) {
return $q(function (resolve, reject) {
$http({
method: 'PUT',
url: Define.TEST_URL + cat_id + cat_url + content_id,
data: item_data
}).then(function (response) {
resolve(response);
}, function (error) {
reject(error);
});
});
}
function getCarList(cat_id, cat_url) {
return $q(function (resolve, reject) {
$http({
method: 'GET',
url: Define.TEST_URL + cat_id + cat_url
}).then(function (response) {
resolve(response);
}, function (error) {
reject(error);
});
});
}
Run Code Online (Sandbox Code Playgroud)
这是getCarList响应
{
"error_msg": "",
"error_num": 0,
"statusText": "OK"
"results": [
{
"car_id": "CAR0121",
"car_name": "AUDI R8"
},
{
"car_id": "CAR0122",
"car_name": "AUDI A6"
},
{
"car_id": "CAR0128",
"car_name": "BENZ"
},
{
"car_id": "CAR0130",
"car_name": "PORCHE"
},
]
}
Run Code Online (Sandbox Code Playgroud)
Angularjs中的$ q.all中是否有方法顺序?
是的,订单是关于你给它的Promises订单 $q.all()
来自ref:$ q.all()
返回将使用值的数组/散列解析的单个promise,每个值对应于promises数组/散列中相同索引/键的promise.如果任何承诺通过拒绝得到解决,则此结果承诺将被拒绝并具有相同的拒绝值.
var promises = [promise1(), promise2(), promise3()];
$q.all(promises).then((values) => {
console.log(values[0]); // value promise1
console.log(values[1]); // value promise2
console.log(values[2]); // value promise3
});
Run Code Online (Sandbox Code Playgroud)
var promises = {one: promise1(), two: promise2(), three: promise3()};
$q.all(promises).then((values) => {
console.log(values.one); // value promise1
console.log(values.two); // value promise2
console.log(values.three); // value promise3
});
Run Code Online (Sandbox Code Playgroud)
但我得到的结果是先获得车名,然后再编辑车名.
我建议你创建地图方法并测试你得到的东西:
$q.all({edit:
HttpService.editItem(
$scope.$parent.category_id, // category id
Define.CAR_CAT, // category url to request
$scope.car_id, // car_id wanna edit
{car_name: inputValue.toUpperCase()} // data
),
getCar: HttpService.getCarList(
$scope.$parent.category_id, // category id
Define.CAR_CAT // category url to request
)
}).then(function (results) {
// results.edit
// results.getCar
});
Run Code Online (Sandbox Code Playgroud)
编辑
如果您希望调用是连续的,则需要使用 Promise 链来调用它们,而不是使用$q.all
HttpService.editItem(
$scope.$parent.category_id, // category id
Define.CAR_CAT, // category url to request
$scope.car_id, // car_id wanna edit
{car_name: inputValue.toUpperCase()} // data
)
.then(function(result) {
if (result.statusText === 'OK') {
return HttpService.getCarList(
$scope.$parent.category_id, // category id
Define.CAR_CAT // category url to request
)
}
else {
return $q.reject();
}
})
.then(function (result) {
if (result.statusText === 'OK') {
.....
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2613 次 |
最近记录: |