# sprang-angular-decorators

> Spring like decorators for angular 1

Latest version **1.0.2** (published 2018-12-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install sprang-angular-decorators
pnpm add sprang-angular-decorators
yarn add sprang-angular-decorators
bun add sprang-angular-decorators
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2018-12-12 |
| First published | 2018-12-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 116.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Aymeric Trotignon |
| Maintainers | astrok8 |
| Keywords | angular, decorators |

## Links

- npm: https://www.npmjs.com/package/sprang-angular-decorators
- Repository: https://github.com/astroK8/sprang-angular-decorators
- Homepage: https://github.com/astroK8/sprang-angular-decorators#readme
- Issues: https://github.com/astroK8/sprang-angular-decorators/issues
- npm.io page: https://npm.io/package/sprang-angular-decorators

## Dependencies (4)

- [lodash](https://npm.io/package/lodash.md) ^4.17.11
- [es6-shim](https://npm.io/package/es6-shim.md) ^0.35.4
- [sprang-decorators](https://npm.io/package/sprang-decorators.md) ^1.0.0
- [sprang-styled-console](https://npm.io/package/sprang-styled-console.md) ^1.0.0

## Alternatives

- [localforage](https://npm.io/package/localforage.md) — 6.2M weekly downloads
- [localforage-observable](https://npm.io/package/localforage-observable.md) — 30.8K weekly downloads
- [@y/y](https://npm.io/package/@y/y.md) — 30.1K weekly downloads
- [@metaobjectsdev/render](https://npm.io/package/@metaobjectsdev/render.md) — 3.5K weekly downloads
- [@ledgerhq/coin-algorand](https://npm.io/package/@ledgerhq/coin-algorand.md) — 1.1K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2018-12-12
- 1.0.1 — 2018-12-10
- 1.0.0 — 2018-12-10

## README

sprang-angular-decorators
==================

Angular decorators for angular 1 inspired by [ng-forward](https://github.com/ngUpgraders/ng-forward) and java/spring annotations

## Bootstrap

```typescript
import { setModule, startAutowiring } from 'sprang-angular-decorators';

let dependencies = [
    'ngMessages',
    'ngAnimate'
];

setModule(angular.module('App', dependencies)
    .run(['$injector', ($injector: ng.auto.IInjectorService) => {
        startAutowiring($injector);
    }])
);

angular.bootstrap(document.documentElement, ['App'], { strictDi: true });
```

## @Autowired

Auto injection of services

```typescript
import { MyService } from './../myService';

export class X {  

    @Autowired(()=>MyService)
    private myService:MyService
    
    @Autowired('$timeout')
  	private $timeout: ng.ITimeoutService;

    constructor() {}  
}
```

Notes:  
  - Angular native services are injected via there name as a string
  - Custom angular services (decorated with @Service) are injected via a function returning corresponding class


## @Service

Registers an angular service

```typescript
@Service()  
export class MyService {}
```


## @Component

Registers an angular component

```typescript
@Component({
    selector:'my-component-selector'  // '[my-component-selector]' for attribute selector
    template?:string,
    templateUrl?:string
    ... // and all other "classic" component config values
})
class MyComponent implements ng.ComponentLifeCycle {
    
    constructor(){}
    
    $onInit() {
    }

    $onChanges(changes:ng.Changes) {

    }

    $doCheck() {
    }

    $onDestroy() {
    }

    $postLink() {
    }
}
```
***Warning !!! $ctrl shall be used in templates => {{$ctrl.x}}*** 

## @Controller

Registers an angular controller

```typescript
@Controller()  
export class MyController {}
```

## @NgElement

Auto injects components HTMLElement 

```typescript
@Controller()  
export class MyComponentOnly {

    @NgElement
    private $element:ng.IAugmentedJQuery;

}
```

## @NgAttrs

Auto injects component attributes (Input or BindString shall be prefered)

```typescript
@Controller()  
export class MyComponentOnly {

    @NgAttrs
    private $Attrs:ng.IAttributes;

}
```

## @NgScope

Auto injects angular scope

```typescript
@Controller()  
export class MyComponentOrController {

    @NgScope
    private scope:ng.IScope;

}
```

## @Input

```typescript
@Component(...)
class X {
    @Input() inputA;
    @Input('customInputB') inputB;

    $onChanges(changes:any) {
        if(changes[inputA].previousValue !== changes[inputA].currentValue) {
            ...
        }
    }
}
```

```html
<elt input-a="scopeVarA" custom-input-b="scopeVarB"></elt>
```

Any modification of scopeVarA and scopeVarB values will be reported respectively on inputA and inputB (but only in this direction)

Modification can be watch via $onChanges lifecycle method

## @Output

```typescript
@Component(...)
@Inject('$element')
class X {

    @Output() outputA = new OutputEmitter('outputA',this);
    @Output('customOutputB') outputB = new OutputEmitter('outputB',this);

    constructor(private $element:ng.IAugmentedJQuery) {}

    ... {
        this.outputA.emit('john');
        this.outputB.emit({name:'doe'})
    }

}
```

```html
<elt (output-a)="handlera($event)" (custom-output-b)="handlerb($event)"></elt>
```

```typescript
scope.handlera=(e)=>{console.log(e)} // -> 'john'
scope.handlerb=(ev)=>{console.log(ev)} // -> {name:'doe'} 
```

***Note: all native events are also available click, mouseover... with (...)="" syntax. e.g: (click)="clickHandler($event)"***

## @BindString

```typescript
@Component(...)
class X {
    @BindString() stringA;
    @BindString('customStringB') stringB;
}
```

```html
<elt string-a="john" custom-string-b="doe"></elt>
```

Will set  

stringA with "john" 
stringB with "doe" 

## @Require

```typescript
@Component(...)
class X {
    Require('ngModel') modelCtrl;
}
```

```html
<elt ng-model="scopeVar"></elt>
```

## @Filter

```typescript
@Filter('filterName')
class X {
  filter(...):any {
    return ...;
  }
}
```

## @SnabbComponent

```typescript
@SnabbComponent
export class SComponentName extends SnabbdomComponent<Props> {
    constructor() {
        super();
    }

    protected view() {
        ...
    }
    ...
}
```

## @NgEventBus

Annotate class that implements event bus

```typescript
@NgEventBus
export class MyBus {
    ...
}
```

## @ListenBus()

```typescript
export class MyComponent {
    ...

    @ListenBus(()=>MyEvent)
    protected myMethod(data:MyEventData) {
        ...
    }
}
```

myMethod will be called each time MyEvent is fired by class annotated with @NgEventBus.
ListenBus decorator will automatically unregister bus listener when component is destroyed.

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