I feel that other answers, while correct, don't quite explain the root of the question: REST
is a subset of HTTP
.
(我觉得其他答案虽然正确,但并不能完全解释问题的根源: REST
是HTTP
一个子集。)
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之外进行的方式。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…