angularjs - AngularJS - 获取.constant中定义的常量的值
问题描述
我有一个 angularJS .constant,它看起来像这样:
(function (angular, undefined) {
'use strict';
angular.module('someModule')
.constant('MyConstants', {
VALUE_ONE: 12345,
VALUE_TWO: VALUE_ONE // it says "VALUE_ONE is undefined"
});
})(angular);
请注意,当我写 : 时
VALUE_TWO: this.VALUE_ONE
,我的测试与味精有关:
类型错误:未定义不是对象(评估“this.VALUE_ONE”)。
如何在 MyConstants 中获取 VALUE_ONE 的值?
解决方案
我认为你不能那样做,我不明白你为什么要那样做。如果您希望 VALUE_TWO 依赖于 VALUE_ONE,那么它不是常量,您不应尝试将其包含在常量中。
如果你想拥有一组常量值,包括注册为 CONSTANTS 的其他值,你可以将它定义为 angular.js服务的属性:
angular.module('someModule')
.factory('MyService', ['MyConstants', function(MyConstants) {
return {
compositeConstant: [MyConstants.VALUE_ONE, 1, 2, 3];
}
}])
推荐阅读
- mysql - 需要获取公共列条目的第一行,但不包括具有特定列值的行
- html - 在 Bootstrap 4 中,是否可以将导航栏品牌居中并将一些文本对齐到品牌的左侧?
- reactjs - 理解 this.function() 和 this.function 之间的区别
- rx.net - 如何在 Rx.Net 中定义 ConcatMap?
- xslt - 当 XSLT 变量中的重复结果时如何返回单个值?
- vba - ShapeRange.Export 方法在 PowerPoint 中不再有效吗?
- html - 如何用三个级别修复“我的下拉菜单”
- node.js - 有没有办法使用 selenium web-driver (nodejs) 获取当前浏览器名称?
- sql - EXCEPT 和 DISTINCT 在哪些情况下不同?
- amazon-web-services - AWS Cloud Formation:如何自动化 EC2 实例克隆/快照