Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将服务/常量注入Angularjs中的提供者_Javascript_Angularjs_Dependency Injection_Provider - Fatal编程技术网

Javascript 将服务/常量注入Angularjs中的提供者

Javascript 将服务/常量注入Angularjs中的提供者,javascript,angularjs,dependency-injection,provider,Javascript,Angularjs,Dependency Injection,Provider,这里的问题是我能够访问getRoutes(),但是我无法访问注入的常量-“配置”。我错过了什么?谢谢 (function () { 'use strict'; var app = angular.module('app'); app.constant('configuration', { PARTIAL_PATH: "/app/components/partials" }); app.module('app', [ 'r

这里的问题是我能够访问getRoutes(),但是我无法访问注入的常量-“配置”。我错过了什么?谢谢

(function () {
    'use strict';

    var app = angular.module('app');

    app.constant('configuration', {
       PARTIAL_PATH: "/app/components/partials"
    });

    app.module('app', [
        'routeService'
    ]);

    var routeServiceModule = angular.module('routeService', ['common']);

    routeServiceModule.provider('routeConfig',function () {

    this.getRoutes = function () {
        return [
            {
                url: '/login',
                config: {
                    title: 'admin',
                    templateUrl: 'app/components/login/login.html'
                }
            }, {
                url: '/',
                config: {
                    templateUrl: 'app/components/dashboard/dashboard.html',
                    title: 'Dashboard'
                }
            }
        ];
    };

    this.$get = ['configuration', function (configuration) {

        var service = {
            getRoutes: getRoutes(),
            configuration: configuration.PARTIAL_PATH
        };

        return service;
    }];

app.config(['$routeProvider', 'routeConfigProvider', function ($routeProvider, routeConfigProvider) {
        //Unable to get the configuration value
        console.log(routeConfigProvider.configuration);
       //Console is returning as "undefined"
        routeConfigProvider.getRoutes().forEach(function(r) {
            $routeProvider.when(r.url, r.config);
        });
        $routeProvider.otherwise({ redirectTo: '/' });
    }
]);
})();

创建了一个plunkr演示:

我认为这与您创建初始模块的方式有关。试试这个:

    var app = angular.module('app', []);

    app.constant('configuration', {
       PARTIAL_PATH: "/app/components/partials"
    });

    var routeServiceModule = angular.module('routeService', ['app']);

我想你可能把路线复杂化了。你可能有一个很好的理由,但我不知道,我可以建议你用这样的方式保持简单:

MyApp.config(function ($routeProvider) {
    $routeProvider.when('/home', {
        templateUrl: 'home.html',
        controller: 'HomeController',
        activeTab: 'home'
    })
};

MyApp.controller('HomeController', function ($route) {
    console.log($route.current.activeTab);
});
我很想知道为什么您可能无法使用此路由模式或故意选择不同的路由模式。

(关于您最后的评论,请与plnkr联系)

结果是意料之中的

在配置时(在app.config()中),您可以访问定义的原始提供程序,这允许您调用“private”方法或字段(testItem1),并将其配置为运行时使用。“私有”,因为它们在运行时不可访问

在运行时(在app.run()和应用程序的其余部分中),当您请求为其编写提供程序的依赖项时,angular injector会将提供程序的$get方法的结果交给您,而不是提供程序本身,因此您无法访问“private”函数


这一页是我的启示之路:

True您必须首先定义一个模块,这意味着提供依赖项数组,否则它将在那一点上失败。感谢您的措辞,我想不出该如何表达它。嘿,伙计们,感谢您的评论。我试过了,但还是有问题。这是plunkr的演示。这背后的原因是。我构建了路由对象,使其具有以下属性:谢谢您的回答。