【问题标题】:Injecting services/constants into provider in Angularjs在Angularjs中将服务/常量注入提供者
【发布时间】:2014-04-20 09:28:09
【问题描述】:

这里的问题是我可以访问 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 演示:http://plnkr.co/edit/2TIqgxMxBJEPbnk2Wk6D?p=preview

【问题讨论】:

    标签: javascript angularjs dependency-injection provider


    【解决方案1】:

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

    结果是预期的。

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

    在运行时(在 app.run() 和您的应用程序的其余部分中),当您请求一个您为其编写提供程序的依赖项时,角度注入器会将您的提供程序的 $get 方法的结果交给您,不是提供者本身,因此您无法访问“私有”功能。

    这个页面是我的启蒙之路:AngularJS: Service vs provider vs factory

    【讨论】:

    • 感谢您的回答。
    【解决方案2】:

    我认为您可能过于复杂了路线。您可能有一个很好的理由,但由于我不知道,我建议您使用类似这样的东西来保持简单:

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

    我很想知道为什么您可能无法使用此路由模式或故意选择不同的方式。

    【讨论】:

    • 这背后的原因是。我将我的路由对象构造为具有以下属性:
    【解决方案3】:

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

        var app = angular.module('app', []);
    
        app.constant('configuration', {
           PARTIAL_PATH: "/app/components/partials"
        });
    
        var routeServiceModule = angular.module('routeService', ['app']);
    

    【讨论】:

    • 没错,你必须首先定义一个模块,这意味着提供依赖数组,否则它将失败。
    • 谢谢你的措辞,我想不出如何措辞。
    • 大家好,感谢您的 cmets。我试过了,但它仍然是一个问题。这是 plunkr 演示。 plnkr.co/edit/2TIqgxMxBJEPbnk2Wk6D?p=preview
    猜你喜欢
    • 2014-01-19
    • 1970-01-01
    • 2016-02-18
    • 2016-05-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    • 2015-06-13
    • 1970-01-01
    相关资源
    最近更新 更多