角度工厂

时间:2017-04-26 15:42:52

标签: javascript angularjs

我正在尝试创建可重用的服务,以便我可以使用以下代码添加,更新,删除等:

get get有效然而我的其他功能出错?为什么呢?

app.factory('requestService',['$http', function ($http) {
// reusable get request
return {
    getRequest: function (url) {
        return $http({
            method: 'GET',
            dataType: "json",
            url: url
        }).then(function successCallback(response) {
            return response;
        }, function errorCallback(response) {
            return response;
        });
    }
};   
return {
    postRequest :function (url, data) {
    $http({
        method: 'POST',
        data: data,
        url: url
    }).then(function successCallback(response) {
        return response;
    }, function errorCallback(response) {
        return response;
    });
}
}
// reusable put request
return {
    putRequest: function (url, data) {
        return $http({
            method: 'PUT',
            data: data,
            url: url
        }).then(function successCallback(response) {
            return response;
        }, function errorCallback(response) {
            return response;
        });
    }
};
// reusable delete request
return {
deleteRequest: function (url) {
        return $http({
            method: 'DELETE',
            url: url
        }).then(function successCallback(response) {
            return response;
        }, function errorCallback(response) {
            return response;
        });
    }
};

当我传入一个url时,get工作,但是当我将数据传递到post并放入函数时,我得到错误requestService.postRequest不是函数。

我通过正确定义它在另一个控制器中调用该函数但是在调用post时它只返回错误。

app.controller('AdminController', function ($scope, $http, requestService) {

我通过执行以下操作调用了post函数:

requestService.postRequest('https://.....',{key:value}).then(function (response) {
console.log(response);
    });

1 个答案:

答案 0 :(得分:1)

我认为你需要把这些函数放在第一个回归中,如下所示:

app.factory('requestService',['$http', function ($http) {
return {
    getRequest: function (url) {
        return $http({
            method: 'GET',
            dataType: "json",
            url: url
        }).then(function successCallback(response) {
            return response;
        }, function errorCallback(response) {
            return response;
        });
    },
    postRequest : function(url, data) {
        $http({
            method: 'POST',
            data: data,
            url: url
        }).then(function successCallback(response) {
            return response;
        }, function errorCallback(response) {
            return response;
        });
    },
    putRequest: function (url, data) {
        return $http({
            method: 'PUT',
            data: data,
            url: url
        }).then(function successCallback(response) {
            return response;
        }, function errorCallback(response) {
            return response;
        });
    },
    deleteRequest: function (url) {
        return $http({
            method: 'DELETE',
            url: url
        }).then(function successCallback(response) {
            return response;
        }, function errorCallback(response) {
            return response;
        });
    }
};

希望它有所帮助!