如何将url传递给angular js factory中的$ resource

问题描述 投票:2回答:2

以前我在角度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
 });

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

这是为$ resource Services代码创建url的代码

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

注意

我在PatientService中有很多方法,所以我不想在patientService中为每个$资源添加额外的功能,这会将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
                    }
                })
            }
            };                                                                                                                                                                                                                                                                                                                                                                                                      
       }]);
angularjs angularjs-service ngresource
2个回答
0
投票

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

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


0
投票

对于上述问题场景,您可以将现有$资源包装在某个函数中,并将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
                    }
                })
            }
            };                                                                                                                                                                                                                                                                                                                                                                                                      
       }]);

有道理 。

© www.soinside.com 2019 - 2024. All rights reserved.