简体   繁体   中英

RestCalls using Promise in angularJs

I am having Rest Call code but i want to make it as a promise(using $q)what changes I need to do. //This is my controller

  $scope.addCustomer=function() {
        alert("inside add Customer function");
        var customer={
            name: $scope.name,
            number: $scope.number
        };
        customerService.addCustomer(customer)
        .then(function(response){
            console.log(response);
        }).catch(function(error){
            console.log(error);
        });
    };

//This is my servic

angular.module('shoppingPad').service('customerService', customerService);

function customerService($http, restService) {
    this.addCustomer = function(customer) {
  return restService.postRequest('customers/info', customer);
    };

//This is my Rest Service code

angular.module('shoppingPad').service('restService',restService);
function restService($http){
    //set port number and baseUrl here
    var port=3001;
    var baseUrl="http://localhost:"+port;

//generic getRequest function 
 this.postRequest=function(url,data,successCallback,failureCallback){
    return $http({
             method: 'POST',
             url: baseUrl+"/"+url,
             data: data
           });
};

$http functions already return Promises

show this page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM