Angular.js常量问题?

时间:2016-04-29 15:18:25

标签: javascript angularjs local-storage constants

我有点困惑,我正在按照本教程试着让真正的工作变得有效。 https://devdactic.com/user-auth-angularjs-ionic/

我被困在第一阶段

这是我的app.js

// create main module, adding dependencies for ionic, angular router, angular  mock and apps own modules

var jailbreak = angular.module('jailbreak', ['ionic', 'ui.router', 'map', 'chat', 'constant'])

jailbreak.run(function(AUTH_EVENTS) {
  document.getElementById('debug').innerHTML = JSON.stringify(AUTH_EVENTS);
})

然后这是我的constants.js

angular
  .module('constant', [])
  .constant('AUTH_EVENTS', {
    notAuthenticated: 'auth-not-authenticated',
    notAuthorized: 'auth-not-authorized'
  })
  .constant('USER_ROLES', {
    admin: 'admin_role',
    public: 'public_role'
  });

2 个答案:

答案 0 :(得分:1)

可能是语法错误,您在模块声明后获得了分号,但之后您尝试将.constant链接到其上。

改变这个:

var constants = angular.module('constant', []);

进入这个:

var constants = angular.module('constant', [])

答案 1 :(得分:1)

当angular尝试引导主模块时,其他模块应该可用: (只是注意,避免全局变量,使用getters访问模块)

angular
  .module('constant', [])
  .constant('AUTH_EVENTS', {
    notAuthenticated: 'auth-not-authenticated',
    notAuthorized: 'auth-not-authorized'
  })
  .constant('USER_ROLES', {
    admin: 'admin_role',
    public: 'public_role'
  });


angular
  .module('jailbreak', [
    /** 'ionic', 'ui.router', 'map', 'chat', **/ 'constant' /**, 'ngMockE2E' **/
  ])
  .run(function(AUTH_EVENTS) {
    document.getElementById('debug').innerHTML = JSON.stringify(AUTH_EVENTS);
  });
#debug {
  margin: 20px;
  padding: 10px;
  border: 1px solid cyan;
  background: rgba(0, 255, 255, .3);
  font-family: monospace;
  word-wrap: break-all;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<section ng-app="jailbreak"></section>
<div id="debug"></div>