如何在angularjs中进行休息调用的通用方法

时间:2014-04-14 10:13:05

标签: angularjs angularjs-directive angular-ui

如何在angularjs中进行休息调用的通用方法?

我已尝试过单一请求,但工作正常

UIAppRoute.controller('test', ['$scope', 'checkStatus', function($scope, checkStatus) {
    $scope.data = {};
    checkStatus.query(function(response) {
      $scope.data.resp = response;
    });
}])



UIAppResource.factory('checkStatus', function($resource){
    return $resource(baseURL + 'status', {}, {'query': {method: 'GET', isArray: false}})
})

我想将此作为所有请求的通用

请分享任何样品,...提前感谢

1 个答案:

答案 0 :(得分:1)

我正在使用这样的东西:

.factory('factoryResource', ['$resource', 'CONF',
    function($resource, CONF) {
        return {
            get: function(endPoint, method) {
                var resource = $resource(CONF.baseUrl + endPoint, {}, {
                    get: {
                        method: method || 'GET'
                    }
                });

                return resource.get().$promise;
            }
        };
    }
])

呼叫:

factoryResource.get(CONF.testEndPoint, "POST"); // make a POST and return a promise and a data object
factoryResource.get(CONF.testEndPoint, "GET"); // make a GETand return a promise and a data object
factoryResource.get(CONF.testEndPoint); // make a GETand return a promise and a data object

配置文件包含:

angular.module('app.constant', [])

.constant('CONF', {
    baseUrl: 'http://localhost:8787',
    testEndPoint: '/api/test'
});