角$ http和服务VS ngResource [英] Angular $http vs service vs ngResource

查看:125
本文介绍了角$ http和服务VS ngResource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在使用简单的$ HTTP请求到服务器和/或包裹在服务与使用ngResource对象(较明显对一个RESTful资源等)的要求,了解优势/劣势。

I would like to understand the advantages / disadvantages over using a simple $http request to a server and/or wrapping that request in a service versus using a ngResource object (other than the obvious regarding a RESTful resource).

从我的理解的$ HTTP请求较低水平,但非常灵活,可配置的,而用一个RESTful API打交道时的ngResource对象使沟通非常简单。

From my understanding the $http requests are low level but very flexible and configurable whereas when dealing with a RESTful API the ngResource objects make communication very simple.

我猜我询问给出一个非常简单的场景,说从服务器检索数据(GET对象数组的请求,说的)是不是更有效只需使用$ http请求,而不是任其包装在服务(这应该永远是这样吗?),或者使用ngResource对象?

I guess what I am enquiring about is given a very simple scenario, say retrieval of data from a server (GET request of array of objects say) is it more efficient to simply use a $http request as opposed to either wrapping it in a service (should this always be the case?) or using an ngResource object?

在这里任何想法,将AP preciated。例如,$ http响应可以被缓存,可以一个ngResource?谢谢你。

Any thoughts here would be appreciated. For example, a $http response can be cached, can an ngResource? Thanks.

推荐答案

决定,我会制定成一个答案,因为这在评论中,我们摸索出你想知道基本上是什么:

Decided I'll formulate this into an answer since in the comments we worked out basically what you wanted to know:

使用$ HTTP或$资源中的结果还是可以被缓存,你指出的那样真的在你的问题用一个比其他的原因。如果你有那么一个RESTful接口使用$资源是更好,因为你最终会少写锅炉板code是共同的一个RESTful接口,如果你不使用RESTful服务则$ HTTP更有意义。您可以缓存数据无论哪种方式<一个href=\"http://www.pseudobry.com/power-up-http-with-caching/\">http://www.pseudobry.com/power-up-http-with-caching/

Using $http or $resource the results can still be cached, you pointed out the reasons to use one over the other really in your question. If you have a RESTful interface then using $resource is better since you'll end up writing less boiler-plate code that is common to a RESTful interface, if you're not using a RESTful service then $http makes more sense. You can cache data either way http://www.pseudobry.com/power-up-http-with-caching/

我觉得把$ HTTP或$资源请求到服务只是一般的作品出来更好,因为你想有机会获得来自多个地点和服务作为一个单独的数据。所以,基本上可以处理任何类型的缓存你想在那里做和控制器都可以只是看相应的服务,以更新自己的数据。我发现,$组合腕表在控制器上的业务数据和返回从我的服务方法的承诺给我带来最大的灵活性,如何在控制器更新的东西。

I think putting $http or $resource requests into a service just generally works out better because you want to have access to the data from multiple locations and the service acts as a singleton. So, basically you can handle any kind of caching you want to do there and controllers can all just watch the appropriate services to update their own data. I've found that a combo of $watch in the controllers for data on the service and returning the promises from my service's methods gives me the most flexibility with how to update things in the controller.

我把这样的事情在具有exampleService注入在控制器定义顶部我的控制器。

I'd put something like this in my controller having the exampleService injected at the top of the controller definition.

angular.module("exampleApp", []).service('exampleService', ["$http", "$q" ,function ($http, $q) {
    var service = {
        returnedData: [],
        dataLoaded:{},
        getData = function(forceRefresh)
        {
            var deferred = $q.defer();

            if(!service.dataLoaded.genericData || forceRefresh)
            {
                $http.get("php/getSomeData.php").success(function(data){
                    //service.returnedData = data;
                    //As Mark mentions in the comments below the line above could be replaced by
                    angular.copy(data, service.returnedData);
                    //if the intention of the watch is just to update the data
                    //in which case the watch is unnecessary and data can
                    //be passed directly from the service to the controller
                    service.dataLoaded.genericData = true;
                    deferred.resolve(service.returnedData);
                });
            }
            else
            {
                deferred.resolve(service.returnedData);
            }

            return deferred.promise;
        },
        addSomeData:function(someDataToAdd)
        {
            $http.post("php/addSomeData.php", someDataToAdd).success(function(data){
                service.getData(true);
            });
        }
    };
    service.getData();
    return service;
}]).controller("ExampleCtrl", ["$scope", "exampleService", function($scope, exampleService){
  //$scope.$watch(function() {return exampleService.returnedData}, function(returnedData){
  //  $scope.myModel.someData = returnedData;
  //});
  //if not using angular.copy() in service just use watch above
  $scope.myModel.someData = exampleService.returnedData;
}]);

另外这里是一个从角团队最佳实践,我还是重新观看,随着时间的推移慢慢吸收了不错的视频。

Also here's a nice video from the Angular team on Best Practices that I'm still re-watching and slowly absorbing over time.

http://www.youtube.com/watch?v=ZhfUv0spHCY

具体的服务VS控制器:
<一href=\"http://www.youtube.com/watch?v=ZhfUv0spHCY&t=26m41s\">http://www.youtube.com/watch?v=ZhfUv0spHCY&t=26m41s

Specifically on services vs controllers: http://www.youtube.com/watch?v=ZhfUv0spHCY&t=26m41s

这篇关于角$ http和服务VS ngResource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆