Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use environment variable in index.html for Angular 6

I am using angular6, in my project I am using Facebook Account Toolkit for mobile verification purpose.

I need to initialise Account toolkit in index.html file using following code.

  AccountKit.init({
   appId:"XX",
   state:"xx",
   version:"v1.2",
   fbAppEventsEnabled:true,
   debug:true
 });

The problem is, values for appId and state change depending on environment (development/test/production).

How can I use environment variables in index.html file.

Please let me know if anyone has a solution for angular 6.

Thanks in advance.

like image 818
Shavareppa Avatar asked Aug 28 '18 05:08

Shavareppa


People also ask

What is use of index html in angular?

html file. Angular is a framework which allows us to create "Single Page Applications", and here the index. html is the single page which was provided by the server. Index.

Can angular access environment variables?

Introduction. If you're building an app that uses an API, you'll want to use your API key for testing environments during development and your API key for live environments during production. In Angular, you can create environment variables with the environment.

Can Angularjs change index html?

Customize Webpack Configuration in Your Angular Application Now that we've added it to our project, we can use the index transform option to modify the HTML file output, based on the environment.


Video Answer


4 Answers

This answer supersedes Artyom's answer for Angular 8 and above. Add the following to your angular.json:

"production": {
  "index": {
    "input": "src/index.someenv.html",
    "output": "index.html"
  },
},
like image 44
jcroll Avatar answered Oct 16 '22 20:10

jcroll


You should create copy of index.html and name it index.someenv.html. Then in your angular.json in environment configuration setup file replacement:

"fileReplacements": [
    {
        "replace": "src/index.html",
        "with": "src/index.someenv.html"
    }
]

The angular cli will replace these files when you run your build

like image 82
Artyom Krasnyuk Avatar answered Oct 16 '22 20:10

Artyom Krasnyuk


An example here for document.write(environment.variable) : https://github.com/angular/angular-cli/issues/4451#issuecomment-285026543

import { environment } from './environments/environment';

if (environment.production) {
  document.write('<script type="text/javascript">// ProductionAnalyticsCodeHere</script>');
} else if (environment.staging) {
  document.write('<script type="text/javascript">// StagingAnalyticsCodeHere</script>');
}
like image 6
Arnaud D Avatar answered Oct 16 '22 21:10

Arnaud D


In main.ts file you can use document.write(environment.variable) and it will write what you want in index.html

(I use it to make the Google Analytics script take a dynamic Tracking ID wether it's in development or production mode, and it works well in Angular6)

like image 5
0x29A Avatar answered Oct 16 '22 19:10

0x29A