how to make global variable in angular

Here is the simplest solution w/o Service nor Observer:

Put the global variables in a file an export them.

//
// ===== File globals.ts    
//
'use strict';

export const sep='/';
export const version: string="22.2.2";    
To use globals in another file use an import statement: import * as myGlobals from 'globals';

Example:

// 
// ===== File heroes.component.ts    
//
import {Component, OnInit} from 'angular2/core';
import {Router} from 'angular2/router';
import {HeroService} from './hero.service';
import {HeroDetailComponent} from './hero-detail.component';
import {Hero} from './hero';
import * as myGlobals from 'globals'; //<==== this one (**Updated**)

export class HeroesComponent implements OnInit {
    public heroes: Hero[];
    public selectedHero: Hero;
    // 
    //
    // Here we access the global var reference.
    //  
    public helloString: string="hello " + myGlobals.sep + " there";

         ...

        }
    }
Thanks @eric-martinez

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
how to declare global variable in angular 11 angular declare variable globaly create a global variable in angular and access local and global variables in angular how to asian the global variable in angular 10 global script in angular create global function and use in html angular how to use a global window variable in angular how to declare global variable in angular how to make a global variable in angular create global variable in angular 11 angular set global variable global variable in angular 8 angular declare global variable create global variable in angular component global variables in anguar global variable in angular without import global function in angular 8 angularjs global variable set a global object in angular angular 11 update a global variable angular 11 global variable angular global variable for all components global variable angularjs angular create a global function global function in angualr 10 global function in angualr angular 5 global variables angular declare global how to declare global variable use hole project in angular 8 angular global variable in html global variable declaration in angular 8 angular 11 set global variables angularjs set global variables global in angular angular access a global variable angular use globale javascript variable angular global variable File declare global variable in angular global variables in html angular 8 global variables angular 8 how to define global variable in angular how to use a global variable in html angular angular use variable global call global variable in angular js how to access global variable in angular js how to use global function in angular js how to declare global variable in angularjs angular export global variable variables globales en angular set global variables in angular universal handle global variables in angular universal handle global variables in angular universal with &quot;angular 9&quot; handle global variables in angular universal with &quot;angular 10&quot; handle global variables in angular universal with 'angular 10' handle global variables in angular universal with angular 10 handle global variables with angular 10 universal set a global variable angular component set a global variable angular global functions in angular how to make declare a variable as global in angular angular 8 how to create global function are there any global reference variables in angular how to define global variable in react js like angular angular define global variable angular create global variable declare var global nodejs.global angular how to set global variable in angularjs how to create global variable in angular variable global angular angular how to set a global variable how to make global varibale in angualr 10 how to make global varibale in angualr can you make a global variable in angular can you make a global varible in angular how to make a global varible inside a angular function how can create a global function in angular js how to make a variable global in angular 8 create global variable file in angular with cli create a global access variable in angular declare globals angular set global variable in angular global variuable in angular angular 11 declare global variable global variable angular 9 global var angular how to declare global variable and send to any component in angular 8 angular 10 global variable angular can global variable access in browser dynamic global variable in angular 7 how to set global variable in anguar and access in another component set global variable angular global variable in angular 11 component angular global variables angular 2 global variable set value as global in angular 8 global var angular 10 how to make global function in angular variables globales angular angular global variable\ how to defince global variable in angular global variable in angular variable global en angular how to make global variables an enviroment varibalble angular global variables in angular 10 use global variable in in 2 component angular angular using global js variable angular global variable how to declare global variable in angular 8 how to use declare variable use like global in whole project in angular 8 how to use declare var in angular use in global variable whole project global variable for application angular display global variable angular on html global variable angular html global value in html angular angular save global variable global variable angular define global variable angular angular how to set a global variable to access it from all the app angular access global variable use global variable component angular how to create global variable in angularjs how to make global variable in angular
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source