Skip to content
A simple global variable service for Angular 2
TypeScript JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.gitignore
.npmignore
LICENSE.md
Readme.md
index.d.ts
index.js
package-lock.json
package.json
tsconfig.json

Readme.md

ng2-simple-global

A simple global variable service for Angular.

To enable faster update, ng2-simple-global switched to Angular CLI starting 8.2.0 and use new repository https://github.com/J-Siu/ng2-simple-global-lib/

Project contains both library and example.

All version < 8.2.0 are in old repository https://github.com/J-Siu/ng2-simple-global/

Index

Install

npm install ng2-simple-global

Usage

Import into Angular 2 application (typescript)

ng2-simple-global is implemented as Angular 2 injectable service name SimpleGlobal.

For module using SimpleGlobal

Add SimpleGlobal into module providers.

import { SimpleGlobal } from 'ng2-simple-global';

@NgModule({
	providers: [SimpleGlobal]
})

For each child component using SimpleGlobal

import {SimpleGlobal} from 'ng2-simple-global';

export class ChildComponent {

	constructor(private sg: SimpleGlobal) { }

}

API

import {SimpleGlobal} from 'ng2-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'];
		}
	}

}

Treat SimpleGlobal instance as a global object and create/assign additional attributes freely, and it will be accessible to all component using the service.

Repository

ng2-simple-global

Example

ng2-simple-global-example

plunker

Contributors

Changelog

  • 1.2.0
    • Support Angular ^2.0.0
    • Clean up package
  • 1.2.1
    • Add license file
    • Add punker example
  • 1.2.2
    • Update package.json
    • Update Readme.md
  • 1.2.3
    • Support Angular2 ^2.4.1
  • 1.2.4
    • Due to the rapid release cycle of Angular, to minimize update purely due to peerDependencies, it is modified as follow: "peerDependencies": { "@angular/core": ">=2.4.0" }
  • 1.2.5
    • Update to support Angular 4.3.1. Please use previous version for Angular 2.x.x.

License

The MIT License

Copyright (c) 2017

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

You can’t perform that action at this time.