Skip to main content

Posts

Showing posts with the label Angular 2 declare global variable Typescript

What is Variable in TypeScript? How to Declare Variable in TypeScript?

The variable is simply a name of storage location and all variables must be identified with unique names and these unique names are called identifiers. Note –   I am using the “ let ” keyword instead of “ var ” keyword. The “ let ” keyword is actually a newer JavaScript construct that TypeScript makes available. Actually, many common problems in JavaScript are reducing by using “ let ” keyword. So we should use “ let ” keyword instead of “ var ” keyword. Stayed Informed – Learn Angular 2 A variable contains values such as "Hi" or 22. When you use the variable, you refer to the data it represents. For example – let name: string = "Hi" ; //var name: string = "Anil Singh"; let num: number = 22; //var num: number = 22; There are some rules while declaring variables i.e. 1.      The variable names must begin with a letter 2.      The variable names can contain letters, digits, underscores, and dollar signs. 3...

What are Types in TypeScript?

What are TypeScript Types? The Type represents the different types of values which are using in the programming languages and it checks the validity of the supplied values before they are manipulated by your programs. Stayed Informed – Learn Angular 2 The TypeScript provides data types as a part of its optional type and its provide us some primitive types as well as a dynamic type “any” and this “any” work like “dynamic”. In TypeScript, we define a variable with a “type” and appending the “variable name” with “colon” followed by the type name i.e. let isActive: boolean = false ; OR var isActive: boolean = false ; let decimal: number = 6 ; OR var decimal: number = 6 ; let hex: number = 0xf00d ; OR var hex: number = 0xf00d ; let name: string = "Anil Singh" ; OR var name: string = "Anil Singh" ; let binary: number = 0 b1010; OR var binary: number = 0 b1010; let octal: number = 0 o744; OR var octal: number = 0 o7...

What is the Best way to Declare and Access a Global Variable in Angular 2?

This post helps us to learn “Declare and Access a Global Variable in Angular 2” using “Typescript” and also share the steps to create and use of this global variables. Stayed Informed - Angular 2 Tutorials and Examples Steps – 1.      Create Global Variables. 2.      Import and Use the Global Variables in the Component. 3.      Result Create Global Variables :- “app.global.ts” import { Injectable } from '@angular/core' ; @Injectable() export class AppGlobals {     readonly baseAppUrl: string = 'http://localhost:57431/' ;     readonly baseAPIUrl: string = 'https://api.github.com/' ; } Import and Use the Global Variables in the Component:- “user.component.ts” import { Component, Injectable} from '@angular/core' ; import { CommonModule } from '@angular/common' ; import { HttpModule, Http } from '@angular/http' ; import { UserService ...