How to Configure Dynamic Environment in angular

Frensol Tech

I usually do “.environment.ts” files to set environments. When i build the code I do 3 builds those are dev, test and production. Single build is not possible for all environments i did two approaches to set environments. I will explain both here.

Method 1:

If you know your Test, Dev and production URLs before build you use this method. This is Static one. First Create a project using below command.

ng new dynamic-envs

Go to newly created project and Create a static service using below command

ng g s services/env1

Then Identify the current url and set your environment according to that.

import { Injectable } from '@angular/core'; import {PlatformLocation } from '@angular/common'; @Injectable({ providedIn: 'root' }) export class Env1Service { constructor(public platformLocation: PlatformLocation) { } getEnv() { /** Identify current url */ const currentURL = (this.platformLocation as any).location.origin; /** set envronment default dev */ let envs = {apiURL:…

View original post 391 more words

Leave a comment