Search code examples
angularangular-cli

Angular 2 / 4 change env variable after build


I read that you can put your environement variable in environement.ts / environement.prod.ts . ok

But this file are compile so I can't change it after the build. Is the anyway to do that ?

(I am using angular-cli)

Thanks


Solution

  • Yup, just like this :

    environment.ts

    export const environment = {
        env: 'Dev'
    };
    

    environment.prod.ts

    export const environment = {
        env: 'Prod'
    };
    

    EDIT What I did to solve that issue is using the window object like so :

    export const environment = {
        env: window['env'] || 'Dev' // you get it for prod
    };
    

    And in the index.html added this :

    <script src="./config.js"></script>
    

    And in the config.js file, the "system guy" can put this :

    window['env'] = 'New environment';
    

    This way, if no config file is provided, the env variable will have a value, and if the config file is provided, it will take the value from it.