i want change pyramid request promises $q.
app.controller('ctrl', function ($scope, $http, $q) { $http.post('http://localhost:1235',data_post1).success(function (data){ console.log("1"); $http.post('http://localhost:1236',data_post2).success(function (data){ console.log("2"); $http.post('http://localhost:1237',data_post3).success(function (data){ console.log("3"); $http.post('http://localhost:1238',data_post4).success(function (data){ console.log("4"); }); }); }); }); } i'm never use $q before.
so best practice pull http requests out of controller
so if had factory
app.factory("foosrvc", ["$q", "$http", function($q, $http){ return { data1: function(postdata){ var deferred = $q.defer(); $http.post("", postdata).success(function(results){ deferred.resolve(results); }); return deferred.promise; }, data2: function(postdata){ var deferred = $q.defer(); $http.post("", postdata).success(function(results){ deferred.resolve(results); }); return deferred.promise; } }; }]); then controller like
app.controller("ctrl", ["$scope", "foosrvc", function($scope, foosrvc){ foosrvc.data1(datatopost).then(function(results){ // here possibly foosrvc.data2(moredatatopost).then(function(moreresults){ // here possibly }); }); }]);
Comments
Post a Comment