【问题标题】:Using angular constant使用角度常数
【发布时间】:2015-12-08 15:20:34
【问题描述】:

如果我在这样的文件中设置一些常量:

'use strict';

angular.module('balrogApp.config', [])
    .constant('balrogConfig', {
        'backend': 'http://127.0.0.1:8000/api/catalog',
        'authenticatedUser': 1
    });

如何从控制器访问它? :

'use strict';

angular.module('balrogApp.header', ['balrogApp.config'])
    .controller('headerController', ['balrogConfig', function ($location, Users, balrogConfig) {
        this.usersList = Users.query();

        this.currentUser = balrogConfig.authenticatedUser;
        /* ... */
    }])

这种方式适用于工厂,但不适用于控制器。那么如何正确导入和使用我的常量呢?

另外,有没有办法从视图中设置一个常量?

基本上,我想在身份验证后使用适当的值(从输入模型的视图中检索)设置authenticatedUser,并能够从任何控制器访问它。

【问题讨论】:

标签: javascript angularjs authentication model-view-controller


【解决方案1】:

您没有正确注入常量。

['$location', 'Users', 'balrogConfig', function ($location, Users, balrogConfig)

【讨论】:

    猜你喜欢
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    • 2014-10-15
    • 2017-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多