Angular $http vs 服务 vs ngResource [英] Angular $http vs service vs ngResource

查看:23
本文介绍了Angular $http vs 服务 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?

这里的任何想法将不胜感激.例如,可以缓存 $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 或 $resource 仍然可以缓存结果,您确实在问题中指出了使用一个而不是另一个的原因.如果您有一个 RESTful 接口,那么使用 $resource 会更好,因为您最终会编写更少的 RESTful 接口常见的样板代码,如果您不使用 RESTful 服务,那么 $http 更有意义.您可以以任何一种方式缓存数据 http://www.pseudobry.com/power-up-http-with-缓存/

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 或 $resource 请求放入服务通常效果更好,因为您希望从多个位置访问数据,并且该服务充当单例.所以,基本上你可以处理你想做的任何类型的缓存,控制器都可以只观察适当的服务来更新他们自己的数据.我发现控制器中的 $watch 组合用于获取有关服务的数据并从我的服务方法中返回承诺,这让我在如何更新控制器中的内容方面具有最大的灵活性.

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;
}]);

还有一个来自 Angular 团队的关于最佳实践的精彩视频,我仍在重新观看并随着时间的推移慢慢吸收.

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

特别是关于服务与控制器:http://www.youtube.com/watch?v=ZhfUv0spHCY&t=26m41s

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

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

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