@Inject decorator for easily annotating dependency-injected class constructors

This commit is contained in:
alecmerdler 2017-01-21 02:56:29 -08:00
parent eea2a18c3f
commit edd7314624
5 changed files with 33 additions and 4 deletions

View file

@ -0,0 +1,17 @@
import { Inject } from './inject.decorator';
describe("Decorator: Inject", () => {
describe("parameter injection", () => {
it("adds given string to the 'inject' property of the annotated class", () => {
expect(ValidService.$inject).toContain('$scope');
});
});
});
class ValidService {
constructor(@Inject('$scope') private $scope: any) {}
}

View file

@ -0,0 +1,11 @@
/**
* Adds the given value to the inject property of the annotated class.
* Used to annotate the constructor parameters of an AngularJS service/component class.
* @param value The string name of the dependency.
*/
export function Inject(value: string) {
return (target: any, propertyKey: string | symbol, parameterIndex: number): void => {
target.$inject = target.$inject || [];
target.$inject[parameterIndex] = value;
}
}