我有一些想要调用的网络服务。$resource或$http,我应该使用哪个?

美元的资源:https://docs.angularjs.org/api/ngResource/service/美元的资源

美元美元http: https://docs.angularjs.org/api/ng/service/ http

在我读了上面的两个API页面后,我迷失了。

你能用通俗易懂的英语给我解释一下它们的区别,以及在什么情况下使用它们?我如何构造这些调用,并将结果正确地读入js对象?


当前回答

我认为重要的是要强调$resource期望对象或数组作为服务器的响应,而不是原始字符串。所以如果你有一个原始字符串(或任何除了对象和数组)作为响应,你必须使用$http

其他回答

我认为答案更多地取决于您在编写代码时的身份。如果你刚接触Angular,可以使用$http,直到你知道为什么需要$resource。除非您有具体的经验,知道$http是如何阻碍您的,并且了解在代码中使用$资源的含义,否则请坚持使用$http。

这是我的经历:我开始了我的第一个Angular项目,我需要向一个RESTful接口发出HTTP请求,所以我做了和你现在做的一样的研究。根据我在这样的SO问题中读到的讨论,我选择使用$resource。我希望我能弥补这个错误。原因如下:

$http examples are plentiful, helpful, and generally just what you need. Clear $resource examples are scarce, and (in my experience) rarely everything you need. For the Angular newbie, you won't realize the implications of your choice until later, when you're stuck puzzling over the documentation and angry that you can't find helpful $resource examples to help you out. $http is probably a 1-to-1 mental map to what you're looking for. You don't have to learn a new concept to understand what you get with $http. $resource brings along a lot of nuance that you don't have a mental map for yet. Oops, did I say you don't have to learn a new concept? As an Angular newbie you do have to learn about promises. $http returns a promise and is .thenable, so it fits right in with the new things you're learning about Angular and promises. $resource, which doesn't return a promise directly, complicates your tentative grasp on Angular fundamentals. $resource is powerful because it condenses the code for RESTful CRUD calls and the transformations for input and output. That's great if you're sick of repeatedly writing code to process the results of $http yourself. To anyone else, $resource adds a cryptic layer of syntax and parameter passing that is confusing.

我希望我3个月前就认识我自己,我会强调地告诉自己:“坚持使用$http的孩子。一切都很好。”

$http用于通用AJAX。在大多数情况下,这是您将使用的。使用$http,您将手动进行GET, POST, DELETE类型调用,并自行处理它们返回的对象。

$resource包装了$http,用于RESTful web API场景。


一般来说:一个基于rest的web服务将是一个数据类型的服务端点,该服务基于HTTP方法(如GET、POST、PUT、DELETE等)对该数据类型执行不同的操作。因此,对于$资源,您可以调用GET以JavaScript对象的形式获取资源,然后更改它并使用POST将其发送回来,甚至使用delete删除它。

... 如果有道理的话。

$http是一个通用的AJAX调用,这意味着它可以包含RESTful api和非RESTful api。

$resource是专门用于该RESTful部分的。

Restful Api是近年来流行起来的,因为url是更好的组织,而不是由程序员随意创建的url。

如果我使用RESTful API来构造url,它将类似于/ API /cars/:carId。

获取数据的方式

angular.module('myApp', ['ngResource'])

    // Service
    .factory('FooService', ['$resource', function($resource) {
        return $resource('/api/cars/:carId')
    }]);

    // Controller
    .controller('MainController', ['FooService', function(FooService){
        var self = this;
        self.cars = FooService.query();
        self.myCar = FooService.get('123');

    }]);

这将为您提供一个资源对象,并自动附带get、save、query、remove、delete方法。

获取数据的方式

angular.module('myApp', [])

    // Service
    .factory('FooService', ['$http', function($http){
        return {
            query: function(){
                return $http.get('/api/cars');
            },

            get: function(){
                return $http.get('/api/cars/123');
            }
            // etc...
        }

看看我们需要如何在RESTFul API上定义每个公共操作。还有一个区别是$http返回promise,而$resource返回一个对象。还有第三方插件可以帮助Angular处理RESTFul API,比如restangular


如果API类似于/ API /getcarsinfo。剩下的就是使用$http。

$resource服务目前不支持promise,因此与$http服务有明显不同的接口。

当涉及到$http或$resource之间的选择时,从技术上讲,没有正确或错误的答案,本质上两者都会做同样的事情。

$resource的目的是允许您传入一个模板字符串(包含占位符的字符串)以及参数值。$resource将模板字符串中的占位符替换为作为对象传递的参数值。这在与RESTFul数据源交互时非常有用,因为它们使用类似的原则来定义url。

$http所做的是执行异步http请求。