Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

javascript - AngularJS $ http和$ resource(AngularJS $http and $resource)

I have some web services that I want to call.

(我有一些我想打电话的网络服务。)

$resource or $http , which one should I use?

($resource$http ,我应该使用哪一个?)

$resource : https://docs.angularjs.org/api/ngResource/service/$resource

($resourcehttps//docs.angularjs.org/api/ngResource/service/$resource)

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

($httphttps//docs.angularjs.org/api/ng/service/$http)

After I read the two above API pages I am lost.

(在我阅读上面两个API页面后,我迷失了。)

Could you please explain to me in plain English what is the difference and in what situation should I use them?

(能否请您用简单的英语向我解释有什么区别,我应该在什么情况下使用它们?)

How do I structure these calls and read the results into js objects correctly?

(如何构造这些调用并将结果正确读入js对象?)

  ask by Tom translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I feel that other answers, while correct, don't quite explain the root of the question: REST is a subset of HTTP .

(我觉得其他答案虽然正确,但并不能完全解释问题的根源: RESTHTTP一个子集。)

This means everything that can be done via REST can be done via HTTP but not everything that can be done via HTTP can be done via REST .

(这意味着可以通过REST完成的所有工作都可以通过HTTP完成,但并非所有可以通过HTTP完成的工作都可以通过REST完成。)

That is why $resource uses $http internally.

(这就是$resource内部使用$http原因。)

So, when to use each other?

(那么,什么时候互相使用?)

If all you need is REST , that is, you are trying to access a RESTful webservice, $resource is going to make it super easy to interact with that webservice.

(如果您只需要REST ,也就是说,您正在尝试访问RESTful Web服务,那么$resource将使得与该Web服务进行交互变得非常容易。)

If instead, you're trying to access ANYTHING that is not a RESTful webservice, you're going to have to go with $http .

(相反,如果您正在尝试访问不是RESTful Web服务的ANYTHING,那么您将不得不使用$http 。)

Keep in mind, you could also access a RESTful webservice via $http , it will just be much more cumbersome than with $resource .

(请记住,您还可以通过$http访问RESTful Web服务,它将比使用$resource更加麻烦。)

This is the way most people have been doing it outside AngularJS, by using jQuery.ajax (equivalent of Angular's $http ).

(这是大多数人通过使用jQuery.ajax (相当于Angular的$http )在AngularJS之外进行的方式。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...