Angular:Provider必须定义工厂方法

时间:2015-08-13 12:28:14

标签: javascript angularjs angular-services

我现在很有新意义和有点失落。我有一个提示器,处理服务器是否发送响应,然后我做一些基于它的东西。

这是提供者代码

define(['angular', '../module'], function (angular, module) {
    return module.provider('httpProvider', ['$httpProvider', function ($httpProvider) {
        var interceptor = ['$q', '$rootScope', function ($q, $rootScope) {
            return {
                response: function (response) {
                    $rootScope.serverError = true;
                    return response;
                },
                responseError: function (rejection) {
                    if (rejection.status === 0) {
                        $rootScope.serverError = true;
                    }

                    return $q.reject(rejection);
                },
            };
        }];

        $httpProvider.interceptors.push(interceptor);
    }]);
});

它会引发错误:

  

提供商' httpProvider'必须定义$ get factory方法。

有什么想法吗?

编辑:

以下是我的工厂现在看起来如何,并且它创建得很好,但我无法将其注入配置

define(['angular', './module'], function (angular, module) {
    module.factory('httpInterceptor', function () {
        var interceptor = ['$q', '$rootScope', function ($q, $rootScope) {
            return {
                response: function (response) {
                    $rootScope.serverError = true;
                    return response;
                },
                responseError: function (rejection) {
                    if (rejection.status === 0) {
                        $rootScope.serverError = true;
                    }

                    return $q.reject(rejection);
                }
            };
        }];

        return interceptor;
    });
});

在模块配置中,我这样使用它:

$httpProvider.interceptors.push('httpInterceptor');

但它实际上只是一个字符串推送到数组(谁会期望这是正确的?D :)并没有发生任何事情。我已经将工厂更改为始终将serverError设置为true,因此我可以对其进行测试,但它实际上什么也不做,因此这意味着永远不会调用response或responseError函数。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

以下是创建拦截器的方法:

angular.module('myApp', []).
    config(function($httpProvider) {
        $httpProvider.interceptors.push('myInterceptor');
    });

angular.module('myApp').
    factory('myInterceptor', function() {
        return {

            //interceptor object

        };
    });

答案 1 :(得分:0)

好的,我已经能够修复它,我发现我已经错误地创建了工厂,正确的是:

define(['angular', './module'], function (angular, module) {
    module.factory('httpInterceptor',['$q', '$rootScope', function ($q, $rootScope) {
            return {
                'request': function(config) {
                    return config;
                },
                'response': function (response) {
                    $rootScope.serverError = false;
                    return response;
                },
                'responseError': function (rejection) {
                    if (rejection.status === 0) {
                        $rootScope.serverError = true;
                    }

                    return $q.reject(rejection);
                }
            };
    }]);
});

在模块配置中我使用:

$httpProvider.interceptors.push('httpInterceptor');

感谢各位的帮助。