扩展AngularJS路由提供程序的语法

时间:2019-05-30 13:01:42

标签: javascript angularjs

希望这个问题不会被错误地标记为重复。

我正在尝试扩展AngularJS $routeProvider,如对先前问题的回答所建议的那样。不幸的是,这不起作用,我的语法可能不正确。

这是我的“顶级”模块的经过清理的版本:

// app.js
angular.module('app', ['ngRoute', 'ngSanitize'
        , 'main'
    ])

    .provider('appRoute', ['$routeProvider', function($routeProvider) {

        this.$get = function($http) {
            console.log("Instantiaing $rmaRouteProvider");
            var universalResolves = {authorize: function($http) {
                    return $http.get('/svc/authorize/view?urlPath=' + path)
                        .then(function(response) {
                            var data = response.data;
                            if (response.data.result === 'NOT_AUTHORIZED') {
                                throw "NOT_AUTHORIZED";
                            }

                            console.log("finished authorized()");
                            return data;
                        });
                }
            };

            var rmaRouteProvider = angular.extend({}, $routeProvider, {
                when: function(path, route) {
                    route.resolve = (route.resolve) ? route.resolve : {};
                    angular.extend(route.resolve, universalResolves);
                    $routeProvider.when(path, route);
                    return this;
                }
            });

            return rmaRouteProvider;
        }
    }]);

这是我的main.js,在这里我尝试使用appRoute提供者而不是$routeProvider来定义路由:

angular.module('admin', ['ngRoute', 'ngSanitize', 'ngCsv'])
.config(['appRouteProvider', function(appRouteProvider) {
       appRouteProvider.when('/route', {

            templateUrl: 'pageTemplate.tmpl.html',
            controller: 'pageCtrl'

       }
]);

不幸的是,我收到以下错误:

Error: [$injector:modulerr] Failed to instantiate module app due to:
[$injector:modulerr] Failed to instantiate module main due to:
[$injector:unpr] Unknown provider: appRouteProvider
http://errors.angularjs.org/1.5.8/$injector/unpr?p0=%24appRouteProvider
minErr/<@https://localhost:8443/js/angular/angular.js:68:12
createInjector/providerCache.$injector<@https://localhost:8443/js/angular/angular.js:4511:19
getService@https://localhost:8443/js/angular/angular.js:4664:39
injectionArgs@https://localhost:8443/js/angular/angular.js:4688:58
invoke@https://localhost:8443/js/angular/angular.js:4710:18
runInvokeQueue@https://localhost:8443/js/angular/angular.js:4611:11
loadModules/<@https://localhost:8443/js/angular/angular.js:4620:11
forEach@https://localhost:8443/js/angular/angular.js:321:11
loadModules@https://localhost:8443/js/angular/angular.js:4601:5
loadModules/<@https://localhost:8443/js/angular/angular.js:4618:40
forEach@http…

正如我所说,我希望这是我的语法错误。

编辑:我意识到提供者的身份特定于加载顺序。 app.js是应用程序index.html文件中第一个导入的<script>,应首先加载。

编辑2:我将此问题更新为以下内容:Custom Route Provider when is not a function

0 个答案:

没有答案