A global variable service for Angular.
ngx-simple-global uses Angular CLI starting 12.2.0.
npm install ngx-simple-global
ngx-simple-global
is implemented as Angular injectable service name SimpleGlobal.
Add SimpleGlobal
into module providers.
import { SimpleGlobal } from 'ngx-simple-global';
@NgModule({
providers: [SimpleGlobal]
})
import {SimpleGlobal} from 'ngx-simple-global';
export class ChildComponent {
constructor(private sg: SimpleGlobal) { }
}
import {SimpleGlobal} from 'ngx-simple-global';
@Component({
selector: 'child-com',
template: `
<p>This is a global variable: {{sg.gv}}</p>
<input type="text" [(ngModel)]="sg.gv">
`
})
export class ChildComponent {
localVar;
constructor(private sg: SimpleGlobal) {
if (this.sg['gv']) {
this.localVar = this.sg['gv'];
}
}
}