-2

How to create the global variables in angular2/4 with data update of each time with a button click.

saran
  • 75
  • 3
  • 13
  • 2
    Hi, welcome to StackOverflow, Please add some description of your problem. Also read the guidelines before posting. Have fun. Also,You can use inputs and output or services for communications between your components – Faizal Shap Aug 01 '17 at 10:12
  • Use a data sharing service. I will google it for you :/ https://stackoverflow.com/questions/35273106/angular2-share-data-between-components-using-services – FAISAL Aug 01 '17 at 10:13
  • You can check this [answer](https://stackoverflow.com/a/44915111/5556177). Also the [doc](https://stackoverflow.com/documentation/angular/10836/sharing-data-among-components#t=201708011230246435098) has multiple examples. – Nehal Aug 01 '17 at 12:31
  • Thanks, Steve and Faisal. Can you guys help me to reload the current view in the service method calling –  saran Aug 01 '17 at 12:31
  • use a service to share the data between components. Services are singleton so, you can import same service object to different components to access common data. – Ajantha Bandara Aug 01 '17 at 12:45

1 Answers1

1

Try this:

Create TS, you can put in any folder. For example /config.ts

export const ConfigService = Object.freeze({
    apiAuthURL: 'http://api.domain.com'
});

To use it, add import on the top:

import { ConfigService } from "./config";

export class DataService {
   constructor(){
       let authURL = ConfigService.apiAuthURL;
    }
}
DanielZ
  • 303
  • 3
  • 14