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

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

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

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

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


当前回答

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

其他回答

$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超过$http是 如果你在。net中使用Web API

$resource被绑定到一个执行单一目的的控制器中。

美元资源(/ user /: userId,{标识:“@ id”});

[HttpGet]
public bool Get(int id)
{
    return "value"
}

public void Post([FromBody]string value)
{
}

public void Put(int id, [FromBody]string value)
{
}

public void Delete(int id)
{
}

而$http可以是任何东西。只需指定url。

http美元。“真火”

[HttpGet]
public bool Authenticate(string email, string password)
{
    return _authenticationService.LogIn(email, password, false);
}

这只是我的个人观点。

我认为答案更多地取决于您在编写代码时的身份。如果你刚接触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的孩子。一切都很好。”

我能理解的是,如果要管理RESTFUL资源,使用$resource将允许您在代码中定义一次资源,并在资源上使用该资源进行多次操作。这增加了代码的可读性和可维护性。

如果你只是想有通用的调用,而不是像post, delete, update这样管理它,你可以使用$http或$resource(随你喜欢)。

资源服务只是用于使用REST apsi的有用服务。 当你使用它时,你不需要编写CRUD方法(创建,读取,更新和删除)

在我看来,资源服务只是一条捷径, 您可以使用HTTP服务做任何事情。