2015-12-30 60 views
1

我已经完成了自定义枚举(请参阅代码),并且希望重复使用以FOOBAR声明的常量,而不必在types阵列中再次写入常量的值。我会怎么做呢?在同一个角度模块中重复使用常量

var myApp = angular.module('app'); 
myApp.constant('MyEnum', { 
FOO: "foo", 
BAR: "bar", 

types: [ 
    {id: 0, value: 'foo', label:"Foo"}, 
    {id: 1, value: 'bar', label:"Bar"} 
] 
}); 

我愿做这样的事情types数组中:

types: [ 
    {id: 0, value: this.FOO, label:"Foo"}, 
    {id: 1, value: this.BAR, label:"Bar"} 
] 

做起来像这给了我的错误。有什么建议么?

回答

2

您将不得不为此使用中间变量。也许把它们存储在关闭中,这样你就不会用不需要的变量来污染范围。例如:

var myApp = angular.module('app'); 
myApp.constant('MyEnum', function() { 

    var __FOO = "foo", 
     __BAR = "bar"; 

    return { 

     FOO: __FOO, 
     BAR: __BAR, 

     types: [ 
      {id: 0, value: __FOO, label: "Foo"}, 
      {id: 1, value: __BAR, label: "Bar"} 
     ] 
    } 
}()); 
+0

谢谢!在中间变量中使用两个下划线是否很常见? – John

+0

@John他们通常意味着“私人”变量。 –

相关问题