简体   繁体   English

在angularJs中使用Promise进行RestCalls

[英]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. 我有Rest Call代码,但我想作为承诺(使用$ q)做我需要做的更改。 //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 $ http函数已经返回Promises

show this page 显示此页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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