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

查看:540
本文介绍了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'
        });
}]);

我在做什么错了?

What I am doing wrong?

推荐答案

的问题是,您想在一个AngularJS的配置块注入一个值对象助手模块,这是不允许的。您只能在注入配置块的常量和供应商。

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:

配置块 - 获取在供应商登记执行
  和配置阶段。只有供应商和常量可以被注入
  成配置模块。这是prevent意外实例
  服务之前就已经完全配置。

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.

运行块 - 获得
  创建喷射之后执行并且用于kickstart的所述
  应用。只有实例和常量可注入运行
  块。这是prevent进一步的系统配置过程
  应用程序的运行时间。

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天全站免登陆