使用角度常数

Ell*_*one 1 javascript authentication model-view-controller angularjs

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

'use strict';

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

如何从控制器访问它?:

'use strict';

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

        this.currentUser = balrogConfig.authenticatedUser;
        /* ... */
    }])
Run Code Online (Sandbox Code Playgroud)

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

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

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

hen*_*eee 5

你没有正确地注入常数.

['$location', 'Users', 'balrogConfig', function ($location, Users, balrogConfig)
Run Code Online (Sandbox Code Playgroud)