Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define global constants

In Angular 1.x you can define constants like this:

angular.module('mainApp.config', [])     .constant('API_ENDPOINT', 'http://127.0.0.1:6666/api/') 

What would be the equivalent in Angular (with TypeScript)?

I just don't want to repeat the API base url over and over again in all my services.

like image 403
AndreFeijo Avatar asked Jan 25 '16 07:01

AndreFeijo


People also ask

How do you define a global constant?

Global Constants. A global constant is a literal value to which you assign a name. Like a global variable, you can access the value of the global constant from any script or 4GL procedure in the application. You set the value for the global constant when you declare it.

What are global constants C++?

Global Constants in C++ C++ global constants have static linkage. This is different than C. If you try to use a global constant in C++ in multiple files you get an unresolved external error. The compiler optimizes global constants out, leaving no space reserved for the variable.

What are global variables and global constants?

Global variables aren't constant (you can change the value of a global variable, but you can only define a constant once). Constants aren't always global (you can declare a constant in a class). Also, global variables can be any type: scalar, array, or object. Constants can only be scalars.

What is global constant in Java?

A constant is a variable whose value cannot change once it has been assigned. Java doesn't have built-in support for constants, but the variable modifiers static and final can be used to effectively create one. Constants can make your program more easily read and understood by others.


2 Answers

Below changes works for me on Angular 2 final version:

export class AppSettings {    public static API_ENDPOINT='http://127.0.0.1:6666/api/'; } 

And then in the service:

import {Http} from 'angular2/http'; import {Message} from '../models/message'; import {Injectable} from 'angular2/core'; import {Observable} from 'rxjs/Observable'; import {AppSettings} from '../appSettings'; import 'rxjs/add/operator/map';  @Injectable() export class MessageService {      constructor(private http: Http) { }      getMessages(): Observable<Message[]> {         return this.http.get(AppSettings.API_ENDPOINT+'/messages')             .map(response => response.json())             .map((messages: Object[]) => {                 return messages.map(message => this.parseData(message));             });     }      private parseData(data): Message {         return new Message(data);     } } 
like image 133
AndreFeijo Avatar answered Sep 21 '22 16:09

AndreFeijo


The solution for the configuration provided by the angular team itself can be found here.

Here is all the relevant code:

1) app.config.ts

import { OpaqueToken } from "@angular/core";  export let APP_CONFIG = new OpaqueToken("app.config");  export interface IAppConfig {     apiEndpoint: string; }  export const AppConfig: IAppConfig = {         apiEndpoint: "http://localhost:15422/api/"     }; 

2) app.module.ts

import { APP_CONFIG, AppConfig } from './app.config';  @NgModule({     providers: [         { provide: APP_CONFIG, useValue: AppConfig }     ] }) 

3) your.service.ts

import { APP_CONFIG, IAppConfig } from './app.config';  @Injectable() export class YourService {      constructor(@Inject(APP_CONFIG) private config: IAppConfig) {              // You can use config.apiEndpoint now     }    } 

Now you can inject the config everywhere without using the string names and with the use of your interface for static checks.

You can of course separate the Interface and the constant further to be able to supply different values in production and development e.g.

like image 32
Ilya Chernomordik Avatar answered Sep 19 '22 16:09

Ilya Chernomordik