"@angular/common": "^5.0.0"
I searched a lot about it and most say to do it that way ... What am I doing wrong?
My intuition says that every time that I build (constructor) another component, the GlobalService will be clean, but I don't know how to do this correctly...
GlobalService
import { Injectable } from '@angular/core';
@Injectable()
export class GlobalService{
private currency;
constructor() {
}
setCurrency(val) {
this.currency = val;
}
getCurrency() {
return this.currency;
}
}
GlobalService Module
@NgModule({
...,
providers:[ GlobalService ]
})
Setting Value (Any Component)
import { GlobalService } from '.path/global.service';
constructor(..., private global: GlobalService) { }
anyMethod(){
this.global.setCurrency('Test Global Service');
console.log(this.global.getCurrency()); // On console => Test Global Service
}
Getting Value (Another Any Component)
import { GlobalService } from '.path/global.service';
constructor(..., private global: GlobalService) { }
anyMethod(){
console.log(this.global.getCurrency()); // On console => undefined
}