Reputation: 6341
I would like to know how to make a JavaScript object globally available.
I have an Angular service, from which I need an access to a public API key stored as an environment variable. My plan was to make a javascript declaration at the top of the <body>
like this:
<script>
MySite.settings = {
PublishableKey = [here I retreive the env variable]
}
</script>
And inside the Angular service, I thought I could access that value by doing:
...
key: MySite.settings.PublishableKey
...
But I get an error that MySite
is not defined.
Any suggestions?
Upvotes: 1
Views: 72
Reputation: 44841
Well, you don't actually define MySite
, just a property of it. Try this, instead:
<script>
var MySite = {
settings : {
PublishableKey : 'some_key'
}
};
</script>
Notice that I used :
instead of =
to set the properties; that's the correct syntax here.
Upvotes: 6