Search code examples
angularangular2-services

How to config different development environment in Angular 2 app


I have a constant file

export class constants {
    public static get API_ENDPOINT(): string { return 'https://dvelopment-server/'; }
}

And I imported it to my service

private _registrationUrl = constants.API_ENDPOINT+'api/v1/register';

How can I change the endpont with server change . I have development server staging server and local server. I want app to detect the environment change.

In my angular 1 app I used envserviceprovider for this. Can I use the same in angular 2 app ?


Solution

  • I have solved the issue by adding a class method

    export class config {
    
        public static getEnvironmentVariable(value) {
            var environment:string;
            var data = {};
            environment = window.location.hostname;
            switch (environment) {
                case'localhost':
                    data = {
                        endPoint: 'https://dev.xxxxx.com/'
                    };
                    break;
                 case 'uat.server.com':
                    data = {
                        endPoint: 'https://uat.xxxxx.com/'
                    };
                    break;
    
                default:
                    data = {
                        endPoint: 'https://dev.xxxx.com/'
                    };
            }
            return data[value];
        }
    }
    

    In my service

    private _loginUrl = config.getEnvironmentVariable('endPoint')+'api/v1/login;