【问题标题】:AngularJS - Getting Module constants from a controllerAngularJS - 从控制器获取模块常量
【发布时间】:2013-06-29 18:49:02
【问题描述】:

我正在尝试构建一个 myApp.config 模块来存储我的应用的一些设置,我编写了一个 config.js 文件:

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

我将它添加到我的 app.js(角度种子)中:

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

我将它添加到 index.html 文件中,现在我试图弄清楚如何在我的控制器中获取它,我尝试了:

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

但我得到了:

未知提供者:myApp.configProvider

我可能在这里做错了什么,有什么想法吗?

【问题讨论】:

  • Asaf 您尝试将模块作为依赖项注入到控制器中,但这是行不通的。一个模块只能是另一个模块的依赖项。

标签: javascript angularjs


【解决方案1】:

我认为在这样的注入中使用模块名称是无效的。不过,您可以简单地按名称注入常量:

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

【讨论】:

  • 正确,有一个全局依赖注入空间,一旦你在任何模块中设置了.constant,你就可以在其他任何地方注入值。
  • 在 Angular 中拥有一个配置文件(用于可自定义的设置)通常是一种明智的方式吗?
  • @Asaf 您可以使用以下 .constant 服务。 angular.module('app').constant('appSettings', { version: '1.0', appName: 'myApp'}); 然后将其注入控制器或 .config 或您想要的任何东西,例如 angular.module('app').controller('simpleCtrl', ['appSettings', function(appSettings) { $scope.valueInIsolatedScope = appSettings.version; }])
【解决方案2】:

我认为最简单的方法是使用对象文字添加一个常量。这适合我认为的大多数应用程序配置用例,因为它支持复杂的配置对象。 constant 步骤也提前运行,before other providers 已注册。

angular.module('myApp').constant('cfg', {
  url: 'https://myapi.com/v1/',
  httpTimeout: 5000
})

要使用它,你只需注入 cfg:

angular.module('myApp').factory('user', function(cfg, $http){
  // cfg and $http together at last
})

【讨论】:

  • 正是我想要的!
  • 我有一个带有用于导航的键代码的常量提供程序,我想将常量提供程序注入控制器(我想避免单独注入每个条目)。那可能吗?我已经注入了一些其他服务。
  • 比:angular.module('myApp').constant('keyCodes', { left: 36, right: 38 })' 和:angular.module('myApp').factory('myComponent', function(keyCodes) { keyCodes.left }) 更难吗?
【解决方案3】:

还应该注意的是,SimplGy 的解决方案意味着“cfg”对象是一个常量,但该对象的属性不是。这意味着,您不能像这样重新分配“cfg”:

cfg = { randomProperty: randomValue };

您可以像这样重新分配“cfg”对象的属性:

cfg.url = 'BrandNewURL.com';
cfg.httpTimeout = 30;

【讨论】:

  • 这没有回答问题。对其他答案的回复应写为 cmets。
  • @KenWayneVanderLinde 向肯道歉。不幸的是,我还没有收集到足够的代表来发表评论,但我认为作者意识到使用 Angulars 'Constant' 可能发生的问题很重要。
  • @MattM 我觉得你很好。将所有这些都放入评论中是不可能的。
【解决方案4】:

查看此示例中常量的使用:

angular
.module('abp001App', ['ngRoute'])
.constant("myConfig", {
    "url": "http://localhost",
    "port": "80"
})
.config(function ($routeProvider) {
    $routeProvider
        .when('/', {
            templateUrl: 'views/main.html',
            controller: 'MainCtrl'
        })
        .otherwise({
            redirectTo: '/'
        });
})
.controller('MainCtrl', function (myConfig) {
    // Do something with myConfig...
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-10
    • 1970-01-01
    相关资源
    最近更新 更多