使用Angular JS访问模块配置中的常量

时间:2015-02-12 11:44:15

标签: angularjs

此页面:https://docs.angularjs.org/guide/providers底部有一个表格,表明配置阶段可以使用常量和提供程序。

当我尝试在配置中使用某个常量时,我​​收到此错误:

Uncaught Error: [$injector:modulerr] Failed to instantiate module testApp due to:
TypeError: undefined is not a function

常量设置如下:

'use strict';

angular.module('services.config', [])
  .constant('configuration', {
    key: 'value';
  });

然后配置是:

   angular
      .module('testApp', ['services.config', 'ngRoute'])
      .config(function ($routeProvider, configuration) {
        $routeProvider.when('/', {
          templateUrl: 'views/main.html',
          controller: 'MainCtrl'
        });


        // do something with configuration.key - causes error


      });

有谁知道我做错了什么?

提前致谢。

1 个答案:

答案 0 :(得分:0)

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

app.constant('configuration', {
    key: 'value'
  });

app.config(function (configuration) {    
    console.log(configuration.key); //value           
      });

app.constant('configuration', {
    key: 'value'
  });

这里是jsFiddle:http://jsfiddle.net/gopinathshiva/0701k7ke/8/

相关问题