简体   繁体   English

如何在AngularJS中用$ http替换$ resource?

[英]How can I replace $resource with $http in AngularJS?

My application has the following $resource calls. 我的应用程序具有以下$ resource调用。 From what I can see this could be replaced with $http. 从我可以看到,这可以用$ http代替。

    $resource('/api/:et/', { et: $scope.data.entityType })
        .save(data, newSuccess, error)
        .$promise.finally(last);

    $resource('/api/:et/:id', { et: $scope.data.entityType })
        .delete({ id: entityId }, deleteSuccess, error)
        .$promise.finally(last);

    $resource('/api/:et/:id', { et: $scope.data.entityType }, { update: { method: 'PUT' } })
        .update({ id: entityId }, data, editSuccess, error)
        .$promise.finally(last);

I have checked the $http documentation but I cannot see how to add the calls to the xxxSuccess, error functions and how to do the .$promise.finally(last). 我已经查看了$ http文档,但看不到如何将调用添加到xxxSuccess,错误函数以及如何执行。$ promise.finally(last)。

Can someone explain how I could replicate this functionality using $http ? 有人可以解释我如何使用$ http复制此功能吗?

$http is for general purpose AJAX . $http用于通用AJAX In most cases this is what you'll be using. 在大多数情况下,这就是您要使用的内容。 With $http you're going to be making GET , POST , DELETE type calls manually and processing the objects they return on your own. 使用$http您将手动进行GETPOSTDELETE类型调用,并自行处理它们返回的对象。

$resource wraps $http for use in RESTful web API scenarios. $resource包装$http以便在RESTful Web API方案中使用。


Syntax 句法

$http({
    method : 'GET',
    url : '/someUrl',
    param : { paramKey : paramValue}, // optional
    headers : 'someHeaders' // optional
    }).success(function(data, status, headers, config)
    {   
    // this callback will be called asynchronously
    // when the response is available
    }).error(function(data, status, headers, config)
    {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
    });

$http Documentation - https://docs.angularjs.org/#!/api/ng/service/$http $ http文档-https : //docs.angularjs.org/#!/ api/ng / service/ $http


Maintaing Promise in $http $ http中维护Promise

app.factory('myService', function($http) {
      var myService = {
        async: function() {
          // $http returns a promise, which has a then function, which also returns a promise
          var promise = $http.get('/someUrl').then(function (response) {
            // The then function here is an opportunity to modify the response
            console.log(response);
            // The return value gets picked up by the then in the controller.
            return response.data;
          });
          // Return the promise to the controller
          return promise;
        }
      };
      return myService;
    });

    app.controller('MainCtrl', function( myService,$scope) {
      // Call the async method and then do stuff with what is returned inside our own then function
      myService.async().then(function(d) {
        $scope.data = d;
      });
    });

Take a look at this article Angular Promises , It will definitely benifit you in acheving such scenerios. 看看这篇文章Angular Promises ,它肯定会为您带来这样的景象。

$resource is a further abstracted version of $http. $ resource是$ http的进一步抽象版本。 If you are already using $response you may find it's not useful to change your logic to use $http. 如果您已经在使用$ response,则可能会发现更改逻辑以使用$ http没有用。 That said - 说-

https://docs.angularjs.org/api/ng/service/$http https://docs.angularjs.org/api/ng/service/$http

General usage The $http service is a function which takes a single argument — a configuration object — that is used to generate an HTTP request and returns a promise with two $http specific methods: success and error. 常规用法$ http服务是一个带有单个参数(配置对象)的函数,该参数用于生成HTTP请求,并通过两个$ http特定方法返回成功的诺言:成功和错误。

$http({method: 'GET', url: '/someUrl'}).
    success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
}).
error(function(data, status, headers, config) {
  // called asynchronously if an error occurs
  // or server returns response with an error status.
});

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

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