Angular Global Variable – How to Declare Global Variable in Angular 9/8?

angular global variable or Declare Global Variable in Angular 9/8 Example.

Angular Global Variable

src/app/common/global-constants.ts
[php]
export class GlobalConstants {
public static apiURL: string = “https://www.itsolutionstuck.com/”;

public static siteTitle: string = “Web Development Tutorials”;
}
[/php]

src/app/app.component.ts
[php]
import { Component, OnInit } from ‘@angular/core’;
import{ GlobalConstants } from ‘./common/global-constants’;

@Component({
selector: ‘app-root’,
templateUrl: ‘./app.component.html’,
styleUrls: [‘./app.component.css’]
})
export class AppComponent implements OnInit{
title = GlobalConstants.siteTitle;

constructor() {
console.log(GlobalConstants.apiURL);
}

ngOnInit() {
console.log(this.title);
}
}
[/php]

Results:
[php]
https://www.itsolutionstuck.com/

Web Development Tutorials

Angular is running in the development mode means dev platform. Call enableProdMode() to enable the Live mode.

Object

Live Reloading enabled.
[/php]

See also :  Angularjs onLoad vs ng-ini - Angularjs Difference between onLoad and ng-init

Leave a Comment