0

$http インターセプターとアプリ構成を別々のファイルに分離することに問題があります。これは私が試したことです:

app.js

angular
.module('myApp', [
    'oc.lazyLoad',
    'ui.router',
    'ui.bootstrap',
    'angular-loading-bar',
])
.config(['$stateProvider', '$urlRouterProvider', '$ocLazyLoadProvider', function ($stateProvider, $urlRouterProvider, $ocLazyLoadProvider) {

    $ocLazyLoadProvider.config({
        debug: false,
        events: true,
    });

    $urlRouterProvider.otherwise('/page/home');

    $stateProvider
        .state('page', {
            url: '/page',
            templateUrl: '../views/pages/main.html',
            resolve: {
                loadMyDirectives: function ($ocLazyLoad) {
                    return $ocLazyLoad.load(
                        {
                            name: 'myApp',
                            files: [
                                'scripts/directives/header/header.js',
                                'scripts/directives/header/header-notification/header-notification.js',
                                'scripts/directives/sidebar/sidebar.js',
                                'scripts/directives/sidebar/sidebar-search/sidebar-search.js'
                            ]
                        })
                        ,
                        $ocLazyLoad.load(
                            {
                                name: 'ngResource',
                                files: ['bower_components/angular-resource/angular-resource.js']
                            }),
                        $ocLazyLoad.load(
                            {
                                name: 'angular-storage',
                                files: ['bower_components/a0-angular-storage/dist/angular-storage.min.js']
                            }) ,
                        $ocLazyLoad.load(
                            {
                                name: 'sjcl',
                                files: ['bower_components/sjcl/sjcl.js']
                            })
                }
            }
        })
        .state('page.home', {
            url: '/home',
            controller: 'MainCtrl',
            templateUrl: '../views/pages/home.html',
            resolve: {
                loadMyFiles: function ($ocLazyLoad) {
                    return $ocLazyLoad.load({
                        name: 'myApp',
                        files: [
                            'scripts/controllers/main.js'
                        ]
                    })
                }
            }
        })
        .state('login', {
            templateUrl: 'views/pages/login.html',
            url: '/login',
            resolve: {
                loadMyFile: function ($ocLazyLoad) {

                    return $ocLazyLoad.load({
                        name: 'myApp',
                        files: [
                            'bower_components/a0-angular-storage/dist/angular-storage.min.js',
                            'scripts/service/userService.js',
                            'scripts/service/authService.js'
                        ]
                    }),

                        $ocLazyLoad.load(
                            {
                                name: 'sjcl',
                                files: ['bower_components/sjcl/sjcl.js']
                            }),
                        $ocLazyLoad.load(
                            {
                                name: 'myApp',
                                files: ['scripts/controllers/login/login.js']
                            })
                }
            }
        })
        .state('page.addMoneyFlow', {
            templateUrl: 'views/pages/add.html',
            url: '/addMoneyFlow'
        })
        .state('page.reports', {
            templateUrl: 'views/pages/reports.html',
            url: '/reports'
        })

}])

authService.js

angular.module('myApp')
.service('AuthInterceptor', function($rootScope) {
    var service = this;

    service.request = function(config) {
        alert('test')
        //config.headers.authorization = "sdjkghsdlfkjghfldkjghsfdkljghdslkjghsdfkjghsdflkjghk";

        return config;
    };

    service.responseError = function(response) {
        if (response.status === 401) {
            $rootScope.$broadcast('unauthorized');
        }
        return response;
    };
}).config(['$httpProvider', function ($httpProvider) {
    $httpProvider.interceptors.push('AuthInterceptor');
}])

login.js

angular.module('myApp')
.controller('LoginController', function ($scope, $http, UserService) {

    var login = $scope;

    login.email = "";
    login.password = "";

    login.login = function () {
        $http.get("http://www.w3schools.com/angular/customers.php")
            .success(function (response) {
                $scope.names = response.records;
            });
    };


})

authService.js のコードを app.js ソースコードの直下に配置するとすべて問題ありませんが、別のファイルに配置すると、エラーは発生しませんが、インターセプターは機能しません。

誰でもこれを手伝ってもらえますか?

4

1 に答える 1

0

あなたのauthService.js下に置くapp.js

このような

<script src="app.js"></script>
<script src="authService.js"></script>
于 2015-09-05T15:08:05.497 に答える