# ng-inject-js

> Integrates InjectJS into Angular to allow service registration with different lifetimes

Latest version **0.1.0** (published 2016-03-18) · ISC license · 0 weekly downloads

## Install

```sh
npm install ng-inject-js
pnpm add ng-inject-js
yarn add ng-inject-js
bun add ng-inject-js
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2016-03-18 |
| First published | 2016-03-18 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Nicolas Straub |
| Maintainers | nstraub |
| Keywords | angular, injectjs, lifetime, management, dependency, injection |

## Links

- npm: https://www.npmjs.com/package/ng-inject-js
- npm.io page: https://npm.io/package/ng-inject-js

## Dependencies (1)

- [inject-js](https://npm.io/package/inject-js.md) ~0.3.1

## Recent versions

- 0.1.0 (latest) — 2016-03-18

## README

In AngularJS, all dependencies (services) are singletons (only one instance of the type exists at any given time during the page's lifecycle). This is normally fine, but there are occasions when we need to have a type instantiated every time it's injected (transient), or a type that is a singleton until our SPA changes its state, i.e. when a user navigates to a different part of the site without reloading our webpage (state). We also might want to have a dependency instantiated for an object and all its dependencies, or have a dependency shared between all dependencies of a controller (or directive).

> The last two are called parent and root respectively, and are not yet implemented. I expect to have them out soon.

# Installation

### Using NPM

    npm install ng-inject-js
    
### Manual

Download ng.inject.js from this repository, also [https://raw.githubusercontent.com/nstraub/injectjs/master/dist/inject.min.js](inject.min.js) from [https://github.com/nstraub/injectjs](injectjs). Make sure you have lodash referenced in your web page. add inject.js and then ng.inject.js, both below your reference to AngularJS.

# Usage

As a third parameter to your service, add a string that represents the lifetime (currently `transient` or `state`) desired. For example:

    angular.module('myApp').service('myService1', [function () {}], 'transient');
    angular.module('myApp').service('myService2', ['myService1', function (myService1) { this.s1 = myService1 }], 'state');
    angular.module('myApp').service('myService3', ['myService1', 'myService1', 'myService2', 'myService2', function (s1, s11, s2, s22) {
        console.log(s1 === s11); // false
        console.log(s1 === s2.s1); // false
        console.log(s11 === s2.s1); // false
        console.log(s2.s1 === s22.s1); // true
        
        this.s2 = s2;
        
    }], 'transient');
    
    angular.module('myApp').service('myService4', ['myService3', 'myService3', function (s3, s33) {
        console.log(s3 === s33); //false
        console.log(s3.s2 === s33.s2) // true, because service2 has the state lifetime
    }]); // no third parameter means singleton handled by angular's injector.

---
_Source: https://npm.io/package/ng-inject-js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
