How to define global variable in Angular 2

,



Here is the example of Angular final version

In the src/app folder  AppSettings.ts file and add the below code

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

Usage

In your component file import AppSettings module

import {AppSettings} from '../appSettings';

../ or ../../ use based your folder structure

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);
    }
}





Related Post


Latest Post


Recent Posts Widget

Make sure to never miss a thing...

Get the latest news from the creative industry along with other creative goodies, conveniently delivered to social media.