AngularJS 依赖注入 module.config 中的值 [英] AngularJS dependency injection of value inside of module.config

查看:30
本文介绍了AngularJS 依赖注入 module.config 中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试为模块设置一些辅助值.尝试了服务和价值,但没有帮助:

Trying to setup some helpers value to the module. Tried with service and value and it didn't help:

var finance = angular.module('finance', ['finance.services'])
    .value("helpers", {
        templatePath: function (name) {
            return '/areas/scripts/finance/templates/' + name + '/index.html';
        }
    })
    .config(['$routeProvider', 'helpers', function ($routeProvider, helpers) {
    $routeProvider.
        when('/', {
            templateUrl: helpers.getTemplatePath('dashboard'),
            controller: DashboardController
        })            
        .when('/people', {
            templateUrl: '/areas/scripts/app/people/index.html',
            controller: PeopleController
        })
        .otherwise({
            redirectTo: '/dashboard'
        });
}]);

我做错了什么?

推荐答案

问题是你试图在 AngularJS 模块的配置块中注入一个值对象 helpers 而这是不允许的.您只能在配置块中注入常量和提供程序.

The problem is that you are trying to inject a value object helpers in the config block of a AngularJS module and this is not allowed. You can only inject constants and providers in the config block.

AngularJS 文档(部分:模块加载和依赖项")提供了见解进入这个:

The AngularJS documentation (section: "Module Loading & Dependencies") gives the insight into this:

模块是配置和运行块的集合,它们得到在引导过程中应用于应用程序.在其最简单的形式模块由两种块的集合组成:

A module is a collection of configuration and run blocks which get applied to the application during the bootstrap process. In its simplest form the module consist of collection of two kinds of blocks:

配置块 - 在提供程序注册期间执行和配置阶段.只能注入提供者和常量进入配置块.这是为了防止意外实例化完全配置之前的服务.

Configuration blocks - get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured.

运行块 - 获取在注入器创建后执行并用于启动应用.只能将实例和常量注入 run块.这是为了防止进一步的系统配置应用程序运行时间.

Run blocks - get executed after the injector is created and are used to kickstart the application. Only instances and constants can be injected into run blocks. This is to prevent further system configuration during application run time.

这篇关于AngularJS 依赖注入 module.config 中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆