Search code examples
angular6account-kit

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.


Solution

  • 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

    UPD: For Angular 8+ use this:

    Add the following to your angular.json:

    "production": {
      "index": {
        "input": "src/index.someenv.html",
        "output": "index.html"
      },
    },