AngularJS - 从控制器获取模块常数 [英] AngularJS - Getting Module constants from a controller

查看:148
本文介绍了AngularJS - 从控制器获取模块常数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个myApp.config模块存储一些设置我的应用程序,我写了一个config.js文件:结果

I'm trying to build a myApp.config module to store some settings for my app, I wrote a config.js file:

angular.module('myApp.config', [])
    .constant('APP_NAME','My Angular App!')
    .constant('APP_VERSION','0.3');

我把它添加到我的app.js(角种子):

I added it to my app.js (angular-seed):

angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives', 'myApp.controllers', 'myApp.config']).

我把它添加到index.html文件,现在我想弄清楚如何让它在我的控制器,我想:

I added it to the index.html file, and now I'm trying to figure out how to get it in my controllers, I tried:

angular.module('myApp.controllers', ['myApp.config'])
  .controller('ListCtrl', ['$scope', 'myApp.config', function($scope, $config) {
    $scope.printme = $config;
  }])

但我发现:

未知提供商:myApp.configProvider< - myApp.config

Unknown provider: myApp.configProvider <- myApp.config

我可能在这里做得不对,任何想法?

I'm probably doing something wrong here, any ideas ?

推荐答案

我不认为它是有效的在这样的注射用模块的名字。你可以简单地通过名字注入的常量,虽然:

I don't think it is valid to use the module name in an injection like that. You can simply inject the constants by name, though:

angular.module('myApp.controllers', ['myApp.config'])
  .controller('ListCtrl', ['$scope', 'APP_NAME', function($scope, appName) {
     $scope.printme = appName;
}]);

这篇关于AngularJS - 从控制器获取模块常数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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