我想用戶工廠對象發出多個http請求。我有一些像如何在我的情況下做出多個http請求?
angular.module('App').factory('myFactory', function($http, $q) {
var service = {};
service.setProduct = function(productObj) {
_productObj = productObj;
}
service.makeRequest = function() {
var deferred = $q.defer();
$http.post('/api/product', _productObj).success(function(data){
var id = data.id
//I am not sure how to chain multiple calls by using promise.
//$http.post('/api/details/, id).success(function(data) {
// console.log(data)
//}
deferred.resolve(data);
}).error(function() {
deferred.reject('error here');
})
return deferred.promise;
}
return service;
});
angular.module('App').controller('productCtrl', ['$scope','$http','myFactory',
function($scope, $http, myFactory) {
myFactory.setProduct(productObj);
myFactory.makeRequest()
.then(function(data) {
console.log(data)
}, function(data) {
alert(data)
})
}
]);
我能夠使用myfactory.makeRequest()來打一個電話,但不知道如何把多個HTTP請求。有人可以幫幫我嗎?謝謝!