In my Angular app, I have a object structure to hold my constant variables. As such:
MY_VARS: {
DEFAULT: 'BLOTTER',
SELECT_A_ROLE: '-- Select a role --'
},
I also have a separate object in this file which holds values that I use for my dropdowns. As Such:
ROLE_TYPES: [
{"value": "DEFAULT", "text":"-- Select a role--"},
{"value":"FOO", "text":"Foo"},
{"value":"BAR", "text":"Bar"}
]
I want to be able to reference values in either MY_VARS
or ROLE_TYPES
as constants so I don’t have to hard-code them. I have tried one suggestion to return as:
[{
"value": function(){ return this.DEFAULT },
"text": "-- Select a Role--"
}
And it compiles but the values are being returned empty.
Does anyone know a good way to reference these vars from within its own, or another object structure in the same file?
Thanks
2
Answers
It seems like you just are not accessing the variables you have set up.
If you have this:
Then it seems you just need to point at those variable names, not strings.
I can’t be totally sure how to access them though since you didn’t post the context of how these variables are stored. Are they class properties, constants, etc?
For example, if these were all in a class:
You can run this code and see the output is as you expect it to be
Editing based on comments below
Ok, so if you have this file named
my-constants.ts
And then in another file you want to use those exported variables, you have to import them like this:
This should be doable.
Here is an example that is not Angular specific, but can easily be used inside an Angular component or service.