无法实例化模块httpProvider.responseInterceptors

时间:2015-01-30 19:45:29

标签: angularjs requirejs angular-ui-router route-provider

当我尝试在AngularJs中使用拦截器时,我得到以下错误,在 ui-router 中使用Requirejs。如果我使用 $ routeProvider (ngRoute),那就完美了。

代码有什么问题?

谢谢。

由于以下原因无法实例化模块devWeb: TypeError:无法读取属性' push'未定义的     在ResponseInterceptor

代码拦截。

(function () {
  "use strict";

  define(
    function ()  {

        var ResponseInterceptor = function ( $httpProvider ) {
            var globalResponseInterceptor = function ( promise ) {

                    var onSuccess = function ( packet ) {

                        if( angular.isString(packet.data) ) {
                            return packet;
                        }
                        return packet;
                    };
                    var onFault = function (fault) {

                        var error = angular.isDefined( fault.error ) ? fault.error :

                            angular.isDefined( fault.status ) ? {
                                code: fault.status,
                                message: "Unexpected Server Error"
                            } : {
                                code: "404",
                                message: "Not Found"
                            };
                        return $$q.reject( error) ;
                    };
                    return promise.then( onSuccess, onFault );
            };
            var registerInterceptor = function ( sessao, $rootScope ,$q ) {
                    $$q         = $q;
                    $scope      = $rootScope;
                    errorModel  = sessao.error;

                    return globalResponseInterceptor;
            };

            $httpProvider.responseInterceptors.push( [ "sessao", "$rootScope", "$q", registerInterceptor ] );

        };

        return [ "$httpProvider", ResponseInterceptor ];
});

}());

实例化的代码。

define([...],
    function ( RouteManagerServico, ResponseInterceptor ) {

        var app, appName = 'devWeb';

        app = angular
                .module( appName, [ "ngCookies", "ui.router", "ngRoute",  "ngSanitize"] )

                .config( RouteManagerServico )
                .config( ResponseInterceptor )

                .run( [ "$rootScope", "$state", "$location", function( $rootScope, $state, $location ) {
                    $rootScope.$state    = $state;
                    $rootScope.$location = $location;
                }] );

        angular.bootstrap( document.getElementsByTagName("body")[0], [ appName ] );


        return app;
    }
);

1 个答案:

答案 0 :(得分:1)

我从角度1.2迁移到1.3时遇到了同样的问题,下面的文章深入探讨了如何解决与拦截器相关的问题。

https://github.com/vkarpov15/thecodebarbarian.com/blob/master/lib/posts/20150124_angularjs_interceptors.md