2016-06-23 09:47:54 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
|
*/
|
|
|
|
|
|
2016-05-31 15:22:59 -07:00
|
|
|
import {stringify} from '../facade/lang';
|
2016-09-12 09:44:20 -07:00
|
|
|
import {makeParamDecorator} from '../util/decorators';
|
2014-10-03 20:34:37 -04:00
|
|
|
|
2014-10-14 16:00:35 -04:00
|
|
|
/**
|
2016-09-12 09:44:20 -07:00
|
|
|
* Type of the Inject decorator / constructor function.
|
2014-10-14 16:00:35 -04:00
|
|
|
*
|
2016-05-25 15:00:05 -07:00
|
|
|
* @stable
|
2014-10-14 16:00:35 -04:00
|
|
|
*/
|
2016-09-12 19:14:17 -07:00
|
|
|
export interface InjectDecorator {
|
2016-09-12 09:44:20 -07:00
|
|
|
/**
|
2016-09-14 09:43:01 -07:00
|
|
|
* @whatItDoes A parameter decorator that specifies a dependency.
|
|
|
|
|
* @howToUse
|
|
|
|
|
* ```
|
2016-09-12 09:44:20 -07:00
|
|
|
* @Injectable()
|
|
|
|
|
* class Car {
|
2016-09-14 09:43:01 -07:00
|
|
|
* constructor(@Inject("MyEngine") public engine:Engine) {}
|
2016-09-12 09:44:20 -07:00
|
|
|
* }
|
2016-09-14 09:43:01 -07:00
|
|
|
* ```
|
2016-09-12 09:44:20 -07:00
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* @description
|
|
|
|
|
* For more details, see the {@linkDocs guide/dependency-injection "Dependency Injection Guide"}.
|
2016-09-12 09:44:20 -07:00
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* ### Example
|
|
|
|
|
*
|
|
|
|
|
* {@example core/di/ts/metadata_spec.ts region='Inject'}
|
2016-09-12 09:44:20 -07:00
|
|
|
*
|
|
|
|
|
* When `@Inject()` is not present, {@link Injector} will use the type annotation of the
|
|
|
|
|
* parameter.
|
|
|
|
|
*
|
|
|
|
|
* ### Example
|
|
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* {@example core/di/ts/metadata_spec.ts region='InjectWithoutDecorator'}
|
2016-09-12 09:44:20 -07:00
|
|
|
*
|
|
|
|
|
* @stable
|
|
|
|
|
*/
|
|
|
|
|
(token: any): any;
|
|
|
|
|
new (token: any): Inject;
|
2014-10-05 16:25:42 -04:00
|
|
|
}
|
|
|
|
|
|
2015-02-27 07:42:51 -08:00
|
|
|
/**
|
2016-09-12 09:44:20 -07:00
|
|
|
* Type of the Inject metadata.
|
2015-04-17 13:01:07 -07:00
|
|
|
*
|
2016-09-12 09:44:20 -07:00
|
|
|
* @stable
|
|
|
|
|
*/
|
|
|
|
|
export interface Inject { token: any; }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Inject decorator and metadata.
|
2015-09-21 14:19:03 -07:00
|
|
|
*
|
2016-05-25 15:00:05 -07:00
|
|
|
* @stable
|
2016-09-12 09:44:20 -07:00
|
|
|
* @Annotation
|
2015-02-27 07:42:51 -08:00
|
|
|
*/
|
2016-09-12 20:30:42 -07:00
|
|
|
export const Inject: InjectDecorator = makeParamDecorator('Inject', [['token', undefined]]);
|
2016-09-12 09:44:20 -07:00
|
|
|
|
2015-02-27 07:42:51 -08:00
|
|
|
|
2014-10-14 16:00:35 -04:00
|
|
|
/**
|
2016-09-12 09:44:20 -07:00
|
|
|
* Type of the Optional decorator / constructor function.
|
|
|
|
|
*
|
2016-05-25 15:00:05 -07:00
|
|
|
* @stable
|
2014-10-14 16:00:35 -04:00
|
|
|
*/
|
2016-09-12 19:14:17 -07:00
|
|
|
export interface OptionalDecorator {
|
2016-09-12 09:44:20 -07:00
|
|
|
/**
|
2016-09-14 09:43:01 -07:00
|
|
|
* @whatItDoes A parameter metadata that marks a dependency as optional.
|
|
|
|
|
* {@link Injector} provides `null` if the dependency is not found.
|
|
|
|
|
* @howToUse
|
|
|
|
|
* ```
|
2016-09-12 09:44:20 -07:00
|
|
|
* @Injectable()
|
|
|
|
|
* class Car {
|
2016-09-14 09:43:01 -07:00
|
|
|
* constructor(@Optional() public engine:Engine) {}
|
2016-09-12 09:44:20 -07:00
|
|
|
* }
|
|
|
|
|
* ```
|
2016-09-14 09:43:01 -07:00
|
|
|
*
|
|
|
|
|
* @description
|
|
|
|
|
* For more details, see the {@linkDocs guide/dependency-injection "Dependency Injection Guide"}.
|
|
|
|
|
*
|
|
|
|
|
* ### Example
|
|
|
|
|
*
|
|
|
|
|
* {@example core/di/ts/metadata_spec.ts region='Optional'}
|
|
|
|
|
*
|
2016-09-12 09:44:20 -07:00
|
|
|
* @stable
|
|
|
|
|
*/
|
|
|
|
|
(): any;
|
|
|
|
|
new (): Optional;
|
2015-03-16 14:43:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-09-12 09:44:20 -07:00
|
|
|
* Type of the Optional metadata.
|
2015-03-16 14:43:22 -07:00
|
|
|
*
|
2016-09-12 09:44:20 -07:00
|
|
|
* @stable
|
|
|
|
|
*/
|
|
|
|
|
export interface Optional {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Optional decorator and metadata.
|
2015-09-21 14:19:03 -07:00
|
|
|
*
|
2016-09-12 09:44:20 -07:00
|
|
|
* @stable
|
|
|
|
|
* @Annotation
|
|
|
|
|
*/
|
2016-09-12 20:30:42 -07:00
|
|
|
export const Optional: OptionalDecorator = makeParamDecorator('Optional', []);
|
2016-09-12 09:44:20 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Type of the Injectable decorator / constructor function.
|
2015-09-21 14:19:03 -07:00
|
|
|
*
|
2016-05-25 15:00:05 -07:00
|
|
|
* @stable
|
2015-03-16 14:43:22 -07:00
|
|
|
*/
|
2016-09-12 19:14:17 -07:00
|
|
|
export interface InjectableDecorator {
|
2016-09-12 09:44:20 -07:00
|
|
|
/**
|
2016-09-14 09:43:01 -07:00
|
|
|
* @whatItDoes A marker metadata that marks a class as available to {@link Injector} for creation.
|
|
|
|
|
* @howToUse
|
|
|
|
|
* ```
|
|
|
|
|
* @Injectable()
|
|
|
|
|
* class Car {}
|
|
|
|
|
* ```
|
2016-09-12 09:44:20 -07:00
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* @description
|
|
|
|
|
* For more details, see the {@linkDocs guide/dependency-injection "Dependency Injection Guide"}.
|
2016-09-12 09:44:20 -07:00
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* ### Example
|
2016-09-12 09:44:20 -07:00
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* {@example core/di/ts/metadata_spec.ts region='Injectable'}
|
2016-09-12 09:44:20 -07:00
|
|
|
*
|
|
|
|
|
* {@link Injector} will throw {@link NoAnnotationError} when trying to instantiate a class that
|
|
|
|
|
* does not have `@Injectable` marker, as shown in the example below.
|
|
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* {@example core/di/ts/metadata_spec.ts region='InjectableThrows'}
|
2016-09-12 09:44:20 -07:00
|
|
|
*
|
|
|
|
|
* @stable
|
|
|
|
|
*/
|
|
|
|
|
(): any;
|
|
|
|
|
new (): Injectable;
|
2015-03-16 14:43:22 -07:00
|
|
|
}
|
2015-06-26 15:59:18 -07:00
|
|
|
|
|
|
|
|
/**
|
2016-09-12 09:44:20 -07:00
|
|
|
* Type of the Injectable metadata.
|
2015-06-26 15:59:18 -07:00
|
|
|
*
|
2016-09-12 09:44:20 -07:00
|
|
|
* @stable
|
|
|
|
|
*/
|
|
|
|
|
export interface Injectable {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Injectable decorator and metadata.
|
2015-06-26 15:59:18 -07:00
|
|
|
*
|
2016-09-12 09:44:20 -07:00
|
|
|
* @stable
|
|
|
|
|
* @Annotation
|
|
|
|
|
*/
|
2016-09-12 20:30:42 -07:00
|
|
|
export const Injectable: InjectableDecorator = makeParamDecorator('Injectable', []);
|
2016-09-12 09:44:20 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Type of the Self decorator / constructor function.
|
2015-06-26 15:59:18 -07:00
|
|
|
*
|
2016-09-12 09:44:20 -07:00
|
|
|
* @stable
|
|
|
|
|
*/
|
2016-09-12 19:14:17 -07:00
|
|
|
export interface SelfDecorator {
|
2016-09-12 09:44:20 -07:00
|
|
|
/**
|
2016-09-14 09:43:01 -07:00
|
|
|
* @whatItDoes Specifies that an {@link Injector} should retrieve a dependency only from itself.
|
|
|
|
|
* @howToUse
|
|
|
|
|
* ```
|
2016-09-12 09:44:20 -07:00
|
|
|
* @Injectable()
|
2016-09-14 09:43:01 -07:00
|
|
|
* class Car {
|
|
|
|
|
* constructor(@Self() public engine:Engine) {}
|
2016-09-12 09:44:20 -07:00
|
|
|
* }
|
2016-09-14 09:43:01 -07:00
|
|
|
* ```
|
2016-09-12 09:44:20 -07:00
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* @description
|
|
|
|
|
* For more details, see the {@linkDocs guide/dependency-injection "Dependency Injection Guide"}.
|
2016-09-12 09:44:20 -07:00
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* ### Example
|
|
|
|
|
*
|
|
|
|
|
* {@example core/di/ts/metadata_spec.ts region='Self'}
|
2016-09-12 09:44:20 -07:00
|
|
|
*
|
|
|
|
|
* @stable
|
|
|
|
|
*/
|
|
|
|
|
(): any;
|
|
|
|
|
new (): Self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Type of the Self metadata.
|
2015-06-26 15:59:18 -07:00
|
|
|
*
|
2016-09-12 09:44:20 -07:00
|
|
|
* @stable
|
|
|
|
|
*/
|
|
|
|
|
export interface Self {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Self decorator and metadata.
|
2015-09-21 14:19:03 -07:00
|
|
|
*
|
2016-09-12 09:44:20 -07:00
|
|
|
* @stable
|
|
|
|
|
* @Annotation
|
|
|
|
|
*/
|
2016-09-12 20:30:42 -07:00
|
|
|
export const Self: SelfDecorator = makeParamDecorator('Self', []);
|
2016-09-12 09:44:20 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Type of the SkipSelf decorator / constructor function.
|
2015-09-21 14:19:03 -07:00
|
|
|
*
|
2016-05-25 15:00:05 -07:00
|
|
|
* @stable
|
2015-06-26 15:59:18 -07:00
|
|
|
*/
|
2016-09-12 19:14:17 -07:00
|
|
|
export interface SkipSelfDecorator {
|
2016-09-12 09:44:20 -07:00
|
|
|
/**
|
2016-09-14 09:43:01 -07:00
|
|
|
* @whatItDoes Specifies that the dependency resolution should start from the parent injector.
|
|
|
|
|
* @howToUse
|
|
|
|
|
* ```
|
2016-09-12 09:44:20 -07:00
|
|
|
* @Injectable()
|
2016-09-14 09:43:01 -07:00
|
|
|
* class Car {
|
|
|
|
|
* constructor(@SkipSelf() public engine:Engine) {}
|
2016-09-12 09:44:20 -07:00
|
|
|
* }
|
2016-09-14 09:43:01 -07:00
|
|
|
* ```
|
2016-09-12 09:44:20 -07:00
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* @description
|
|
|
|
|
* For more details, see the {@linkDocs guide/dependency-injection "Dependency Injection Guide"}.
|
|
|
|
|
*
|
|
|
|
|
* ### Example
|
|
|
|
|
*
|
|
|
|
|
* {@example core/di/ts/metadata_spec.ts region='SkipSelf'}
|
2016-09-12 09:44:20 -07:00
|
|
|
*
|
|
|
|
|
* @stable
|
|
|
|
|
*/
|
|
|
|
|
(): any;
|
|
|
|
|
new (): SkipSelf;
|
2015-06-26 15:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-09-12 09:44:20 -07:00
|
|
|
* Type of the SkipSelf metadata.
|
2015-06-26 15:59:18 -07:00
|
|
|
*
|
2016-09-12 09:44:20 -07:00
|
|
|
* @stable
|
|
|
|
|
*/
|
|
|
|
|
export interface SkipSelf {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* SkipSelf decorator and metadata.
|
2015-06-26 15:59:18 -07:00
|
|
|
*
|
2016-09-12 09:44:20 -07:00
|
|
|
* @stable
|
|
|
|
|
* @Annotation
|
|
|
|
|
*/
|
2016-09-12 20:30:42 -07:00
|
|
|
export const SkipSelf: SkipSelfDecorator = makeParamDecorator('SkipSelf', []);
|
2016-09-12 09:44:20 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Type of the Host decorator / constructor function.
|
2015-09-21 14:19:03 -07:00
|
|
|
*
|
2016-05-25 15:00:05 -07:00
|
|
|
* @stable
|
2015-06-26 15:59:18 -07:00
|
|
|
*/
|
2016-09-12 19:14:17 -07:00
|
|
|
export interface HostDecorator {
|
2016-09-12 09:44:20 -07:00
|
|
|
/**
|
2016-09-14 09:43:01 -07:00
|
|
|
* @whatItDoes Specifies that an injector should retrieve a dependency from any injector until
|
|
|
|
|
* reaching the
|
|
|
|
|
* host element of the current component.
|
|
|
|
|
* @howToUse
|
|
|
|
|
* ```
|
|
|
|
|
* @Injectable()
|
|
|
|
|
* class Car {
|
|
|
|
|
* constructor(@Host() public engine:Engine) {}
|
|
|
|
|
* }
|
|
|
|
|
* ```
|
2016-09-12 09:44:20 -07:00
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* @description
|
|
|
|
|
* For more details, see the {@linkDocs guide/dependency-injection "Dependency Injection Guide"}.
|
2016-09-12 09:44:20 -07:00
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* ### Example
|
2016-09-12 09:44:20 -07:00
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* {@example core/di/ts/metadata_spec.ts region='Host'}
|
2016-09-12 09:44:20 -07:00
|
|
|
*
|
|
|
|
|
* @stable
|
|
|
|
|
*/
|
|
|
|
|
(): any;
|
|
|
|
|
new (): Host;
|
2015-06-26 15:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-09-12 09:44:20 -07:00
|
|
|
* Type of the Host metadata.
|
|
|
|
|
*
|
|
|
|
|
* @stable
|
|
|
|
|
*/
|
|
|
|
|
export interface Host {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Host decorator and metadata.
|
|
|
|
|
*
|
|
|
|
|
* @stable
|
|
|
|
|
* @Annotation
|
|
|
|
|
*/
|
2016-09-12 20:30:42 -07:00
|
|
|
export const Host: HostDecorator = makeParamDecorator('Host', []);
|