angularjs - $http POST response from service to controller -
angularjs - $http POST response from service to controller -
how response service in below case??
service:
app.factory('ajaxservice', function($http) { updatetododetail: function(postdetail){ $http({ method: "post", headers: {'content-type': 'application/x-www-form-urlencoded'}, url: post_url, data: $.param({detail: postdetail}) }) .success(function(response){ //return response; }); } })
controller:
updated_details = 'xyz'; ajaxservice.updatetododetail(updated_details);
in th above case, post info through controller , working fine want response come in controller.
how achive that??
$http
returns promise:
return promise
updatetododetail: function(postdetail){ homecoming $http({ method: "post", headers: {'content-type': 'application/x-www-form-urlencoded'}, url: post_url, data: $.param({detail: postdetail}) });
so can
ajaxservice.updatetododetail(updated_details).success(function(result) { $scope.result = result //or whatever else. }
alternatively can pass success
function updatetododetail
:
updatetododetail: function(postdetail, callback){ $http({ method: "post", headers: {'content-type': 'application/x-www-form-urlencoded'}, url: post_url, data: $.param({detail: postdetail}) }) .success(callback);
so controller has
ajaxservice.updatetododetail(updated_details, function(result) { $scope.result = result //or whatever else. })
i prefer first alternative handle errors etc without passing in functions too.
(nb: haven't tested code above might require modification)
angularjs angular-http
Comments
Post a Comment