【问题标题】:How to pass url to $resource which is present in angular js factory如何将 url 传递给 Angular js 工厂中存在的 $resource
【发布时间】:2019-08-20 13:15:48
【问题描述】:

以前我在 Angular js 中通过一些服务传递 url。现在我想将 url 作为参数从控制器传递给 $resource。

我试图将 url 从控制器传递给资源,但它抛出了找不到 url 对象的错误。

以下是我目前的工厂代码:

angular.module('Services').factory('PatientsService', ['$resource', 'Service', '$http',

    function(Resource, Service, Http) {

        return {
            testGetPatients : Resource(Service.getPatientsCustomRefURL(), {}, {
                query : {
                    method : 'GET',
                    isArray : false
                }
            })
        };                                                                                                                                                                                                                                                                                                                                                                                                      
}]);

在上面的代码中,我从 Service.getPatientCUstomRefURL

发送 url 参数

对 $resource 的实际调用如下所示:

PatientsService.getPatientsRef.query(function(refResponse) { 
//TODO for response
});

现在我想传递这样的参数:

PatientsService.getPatientsRef.query("/patient/list",function(refResponse) {
//TODO for response
 });

我应该在我的 PatientService 工厂中进行哪些更改,以便它支持将 url 作为参数传递。

这里是为 $resource 创建 url 的代码 服务代码

angular.module('Services', ['ngResource']).factory('Service', ['$resource', '$location', '$rootScope',
                                                               function($resource, $location, $rootScope) {
    return {
        getPatientsCustomRefURL: function() {
            return '/patient/list';
        }
    };
}
]);

注意

我在 PatientService 中有这么多方法,所以我不想在 PatientService 中为每个 $resource 添加额外的函数,它将 url 作为参数传递,如

angular.module('Services').factory('PatientsService', ['$resource', 'Service', '$http',
        function(Resource, Service, Http) {

            return {
                testGetPatients : function(url){
                 return Resource(url, {}, {
                    query : {
                        method : 'GET',
                        isArray : false
                    }
                })
            }
            };                                                                                                                                                                                                                                                                                                                                                                                                      
       }]);

【问题讨论】:

  • Service.getPatientsCustomRefURL() 返回的值是多少?你能在返回之前做一个console.log(Service.getPatientsCustomRefURL()) 并检查值吗
  • Service.getPatientsCustomRefURL() 将返回一个字符串 url
  • 你能和console.log联系吗?也许它返回一个承诺或未定义
  • @Fraction 我通过添加 Service.getPatientsCustomRefURL() 代码更新了我的问题。并感谢您的快速回复
  • @Fraction 哎呀,我复制了场景以展示整个项目的演示。你是对的,它应该是服务中的 getPatientCUstomRefURL。再次更新问题。现在你找到完美的场景了吗?感谢回复

标签: angularjs angularjs-service ngresource


【解决方案1】:

现在我想传递这样的参数:

PatientsService.getPatientsRef.query("/patient/list",function(refResponse) {
//TODO for response
 });

对于这样的 API 调用,使用$http 服务会更容易:

$http.get("/patient/list").then(function(response) {
    var refResponse = response.data;
    //TODO for response
});

使用$resource 服务改变 url 的标准方法是在 url 模板中定义参数:

var service = $resource("/api/:slug/list", {slug: patient}, {
    getByID: {
        url: "/api/:slug/:id",
        method: "GET",
        isArray: false,
   }
);

例子:

$scope.patients = service.query(); //returns array from
//  api/patient/list 

$scope.doctors = service.query({slug: doctor}); //returns array from
//  api/doctor/list

$scope.patientRecord = service.getById({id: 1234}); //returns object from
//  api/patient/1234

$scope.patientRecord = service.get({id:1234}); //returns object from
//  api/patient/list?id=1234

参数化 URL 模板使用前缀为 : 的参数,如 /user/:username。如果您使用的 URL 带有端口号(例如 http://example.com:8080/api),则会受到尊重。

参数对象中的每个键值首先绑定到 url 模板(如果存在),然后将任何多余的键附加到 ? 之后的 url 搜索查询。

重要的是要意识到调用 $resource 对象方法会立即返回一个空引用(对象或数组取决于isArray)。从服务器返回数据后,现有参考将填充实际数据。这是一个有用的技巧,因为通常将资源分配给模型,然后由视图呈现。

更多信息请见AngularJS $resource API reference - Arguments

【讨论】:

  • 有什么办法可以改变现有的$resource,因为我已经多次使用$resource,您的解决方案需要更改所有代码。感谢您的回复。
  • 在底层,$resource 服务使用 $http 服务。 $resource 工厂旨在促进与RESTful 服务器端数据源的交互。如果您的服务器端数据 API 不是 RESTful,最好使用 $http 服务访问它。我个人不喜欢“返回空对象,稍后填充”策略。我更喜欢返回承诺的服务。
【解决方案2】:

对于上述问题场景,您可以将现有的 $resource 包装在某个函数中并将 url 传递给该函数。

angular.module('Services').factory('PatientsService', ['$resource', 'Service', '$http',
        function(Resource, Service, Http) {

            return {
                testGetPatients : function(url){
                 return Resource(url, {}, {
                    query : {
                        method : 'GET',
                        isArray : false
                    }
                })
            }
            };                                                                                                                                                                                                                                                                                                                                                                                                      
       }]);

有道理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多