Friday, 5 September 2025

Using Config File in Angular App

To use a configuration file in an Angular project, do these steps below. By default, an Angular project uses 'environments' folder name to put the config files.

First, create environment.ts for development or environment.prod.ts for production then add the config values:
// src/environments/environment.ts
export const environment = {
  production: false,
  apiBaseUrl: 'https://your-dev-url/'
};

// src/environments/environment.prod.ts
export const environment = {
  production: true,
  apiBaseUrl: 'https://your-production-url/'
};

Then we can use the config variable anywhere in our project:
import { environment } from '../../environments/environment'; // import the config files

. . .

private webApiUrl = environment.apiBaseUrl; // use environment.VARIABLE_NAME to refer to a config variable

Using 'ng-serve' will use the dev value, while 'ng build --prod' will use the production value.

1 comment:

python trainer said...

TypeScript has become a vital asset for web developers who want to enhance the front-end functionality of traditional blogging systems. As a strongly typed superset of JavaScript, it allows developers to write clean, predictable code with strict type-checking, preventing common runtime errors before they ever hit production. When building custom components—such as dynamic content sliders, advanced search bars, or interactive widgets—TypeScript ensures that data flowing through the theme remains consistent and manageable. TypeScript Course in ChennaiThis structural reliability makes it much easier to maintain and update complex site architectures over time.