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-06-08 16:38:52 -07:00
|
|
|
import {AnimationEntryMetadata} from '../animation/metadata';
|
|
|
|
|
import {ChangeDetectionStrategy} from '../change_detection/constants';
|
2016-04-28 17:50:03 -07:00
|
|
|
import {InjectableMetadata} from '../di/metadata';
|
2016-06-08 16:38:52 -07:00
|
|
|
import {Type, isPresent} from '../facade/lang';
|
|
|
|
|
|
2016-04-28 17:50:03 -07:00
|
|
|
import {ViewEncapsulation} from './view';
|
2016-06-08 16:38:52 -07:00
|
|
|
|
2016-07-29 02:10:30 -07:00
|
|
|
/**
|
|
|
|
|
* Interface for creating {@link DirectiveMetadata}
|
|
|
|
|
* @experimental
|
|
|
|
|
*/
|
|
|
|
|
export interface DirectiveMetadataType {
|
|
|
|
|
selector?: string;
|
|
|
|
|
properties?: string[];
|
|
|
|
|
inputs?: string[];
|
|
|
|
|
events?: string[];
|
|
|
|
|
outputs?: string[];
|
|
|
|
|
host?: {[key: string]: string};
|
|
|
|
|
providers?: any[];
|
|
|
|
|
exportAs?: string;
|
|
|
|
|
queries?: {[key: string]: any};
|
|
|
|
|
}
|
2014-11-21 21:19:23 -08:00
|
|
|
|
2015-03-13 21:54:19 +00:00
|
|
|
/**
|
2015-03-17 23:29:27 +00:00
|
|
|
* Directives allow you to attach behavior to elements in the DOM.
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-08-14 10:03:45 -07:00
|
|
|
* {@link DirectiveMetadata}s with an embedded view are called {@link ComponentMetadata}s.
|
2015-03-17 23:29:27 +00:00
|
|
|
*
|
2015-05-20 09:48:15 -07:00
|
|
|
* A directive consists of a single directive annotation and a controller class. When the
|
|
|
|
|
* directive's `selector` matches
|
2015-03-17 23:29:27 +00:00
|
|
|
* elements in the DOM, the following steps occur:
|
|
|
|
|
*
|
2015-05-20 09:48:15 -07:00
|
|
|
* 1. For each directive, the `ElementInjector` attempts to resolve the directive's constructor
|
|
|
|
|
* arguments.
|
|
|
|
|
* 2. Angular instantiates directives for each matched element using `ElementInjector` in a
|
|
|
|
|
* depth-first order,
|
2015-03-20 21:11:58 +00:00
|
|
|
* as declared in the HTML.
|
2015-03-17 23:29:27 +00:00
|
|
|
*
|
2015-03-19 16:56:45 +00:00
|
|
|
* ## Understanding How Injection Works
|
2015-03-20 21:11:58 +00:00
|
|
|
*
|
2015-03-19 16:56:45 +00:00
|
|
|
* There are three stages of injection resolution.
|
2015-03-20 21:11:58 +00:00
|
|
|
* - *Pre-existing Injectors*:
|
2015-05-20 09:48:15 -07:00
|
|
|
* - The terminal {@link Injector} cannot resolve dependencies. It either throws an error or, if
|
|
|
|
|
* the dependency was
|
2015-03-19 16:56:45 +00:00
|
|
|
* specified as `@Optional`, returns `null`.
|
2015-05-20 09:48:15 -07:00
|
|
|
* - The platform injector resolves browser singleton resources, such as: cookies, title,
|
|
|
|
|
* location, and others.
|
|
|
|
|
* - *Component Injectors*: Each component instance has its own {@link Injector}, and they follow
|
|
|
|
|
* the same parent-child hierarchy
|
2015-05-04 16:50:50 +01:00
|
|
|
* as the component instances in the DOM.
|
2015-05-20 09:48:15 -07:00
|
|
|
* - *Element Injectors*: Each component instance has a Shadow DOM. Within the Shadow DOM each
|
|
|
|
|
* element has an `ElementInjector`
|
2015-04-03 22:07:15 +02:00
|
|
|
* which follow the same parent-child hierarchy as the DOM elements themselves.
|
2015-03-20 21:11:58 +00:00
|
|
|
*
|
2015-05-20 09:48:15 -07:00
|
|
|
* When a template is instantiated, it also must instantiate the corresponding directives in a
|
|
|
|
|
* depth-first order. The
|
2015-04-17 13:01:07 -07:00
|
|
|
* current `ElementInjector` resolves the constructor dependencies for each directive.
|
2015-03-20 21:11:58 +00:00
|
|
|
*
|
2015-05-20 09:48:15 -07:00
|
|
|
* Angular then resolves dependencies as follows, according to the order in which they appear in the
|
2016-07-28 06:31:26 -07:00
|
|
|
* {@link ComponentMetadata}:
|
2015-03-20 21:11:58 +00:00
|
|
|
*
|
|
|
|
|
* 1. Dependencies on the current element
|
|
|
|
|
* 2. Dependencies on element injectors and their parents until it encounters a Shadow DOM boundary
|
|
|
|
|
* 3. Dependencies on component injectors and their parents until it encounters the root component
|
|
|
|
|
* 4. Dependencies on pre-existing injectors
|
|
|
|
|
*
|
|
|
|
|
*
|
2015-05-20 09:48:15 -07:00
|
|
|
* The `ElementInjector` can inject other directives, element-specific special objects, or it can
|
|
|
|
|
* delegate to the parent
|
2015-03-19 16:56:45 +00:00
|
|
|
* injector.
|
2015-03-20 21:11:58 +00:00
|
|
|
*
|
2015-03-19 16:56:45 +00:00
|
|
|
* To inject other directives, declare the constructor parameter as:
|
2015-03-20 21:11:58 +00:00
|
|
|
* - `directive:DirectiveType`: a directive on the current element only
|
2015-07-29 11:26:09 -07:00
|
|
|
* - `@Host() directive:DirectiveType`: any directive that matches the type between the current
|
2015-05-20 09:48:15 -07:00
|
|
|
* element and the
|
2015-07-29 11:26:09 -07:00
|
|
|
* Shadow DOM root.
|
2015-05-20 09:48:15 -07:00
|
|
|
* - `@Query(DirectiveType) query:QueryList<DirectiveType>`: A live collection of direct child
|
|
|
|
|
* directives.
|
|
|
|
|
* - `@QueryDescendants(DirectiveType) query:QueryList<DirectiveType>`: A live collection of any
|
|
|
|
|
* child directives.
|
2015-03-20 21:11:58 +00:00
|
|
|
*
|
2015-03-19 16:56:45 +00:00
|
|
|
* To inject element-specific special objects, declare the constructor parameter as:
|
2015-04-28 11:20:01 -07:00
|
|
|
* - `element: ElementRef` to obtain a reference to logical element in the view.
|
2015-06-01 21:36:06 -07:00
|
|
|
* - `viewContainer: ViewContainerRef` to control child template instantiation, for
|
2015-08-14 10:03:45 -07:00
|
|
|
* {@link DirectiveMetadata} directives only
|
2015-03-20 21:11:58 +00:00
|
|
|
* - `bindingPropagation: BindingPropagation` to control change detection in a more granular way.
|
|
|
|
|
*
|
2015-10-19 15:37:32 +01:00
|
|
|
* ### Example
|
2015-03-17 23:29:27 +00:00
|
|
|
*
|
2015-05-20 09:48:15 -07:00
|
|
|
* The following example demonstrates how dependency injection resolves constructor arguments in
|
|
|
|
|
* practice.
|
2015-03-17 23:29:27 +00:00
|
|
|
*
|
|
|
|
|
*
|
2015-03-20 21:11:58 +00:00
|
|
|
* Assume this HTML template:
|
2015-03-17 23:29:27 +00:00
|
|
|
*
|
|
|
|
|
* ```
|
2015-03-19 16:56:45 +00:00
|
|
|
* <div dependency="1">
|
|
|
|
|
* <div dependency="2">
|
|
|
|
|
* <div dependency="3" my-directive>
|
|
|
|
|
* <div dependency="4">
|
|
|
|
|
* <div dependency="5"></div>
|
2015-03-17 23:29:27 +00:00
|
|
|
* </div>
|
2015-03-19 16:56:45 +00:00
|
|
|
* <div dependency="6"></div>
|
2015-03-17 23:29:27 +00:00
|
|
|
* </div>
|
|
|
|
|
* </div>
|
|
|
|
|
* </div>
|
|
|
|
|
* ```
|
|
|
|
|
*
|
2015-03-19 16:56:45 +00:00
|
|
|
* With the following `dependency` decorator and `SomeService` injectable class.
|
2015-03-17 23:29:27 +00:00
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
* @Injectable()
|
|
|
|
|
* class SomeService {
|
|
|
|
|
* }
|
|
|
|
|
*
|
2015-04-30 13:38:40 -07:00
|
|
|
* @Directive({
|
2015-03-19 16:56:45 +00:00
|
|
|
* selector: '[dependency]',
|
2015-09-30 20:59:23 -07:00
|
|
|
* inputs: [
|
2015-05-26 15:54:10 +02:00
|
|
|
* 'id: dependency'
|
|
|
|
|
* ]
|
2015-03-17 23:29:27 +00:00
|
|
|
* })
|
2015-03-19 16:56:45 +00:00
|
|
|
* class Dependency {
|
2015-03-17 23:29:27 +00:00
|
|
|
* id:string;
|
|
|
|
|
* }
|
|
|
|
|
* ```
|
|
|
|
|
*
|
2015-03-19 16:56:45 +00:00
|
|
|
* Let's step through the different ways in which `MyDirective` could be declared...
|
2015-03-17 23:29:27 +00:00
|
|
|
*
|
2015-03-20 21:11:58 +00:00
|
|
|
*
|
2015-03-17 23:29:27 +00:00
|
|
|
* ### No injection
|
|
|
|
|
*
|
2015-05-20 09:48:15 -07:00
|
|
|
* Here the constructor is declared with no arguments, therefore nothing is injected into
|
|
|
|
|
* `MyDirective`.
|
2015-03-17 23:29:27 +00:00
|
|
|
*
|
|
|
|
|
* ```
|
2015-04-30 13:38:40 -07:00
|
|
|
* @Directive({ selector: '[my-directive]' })
|
2015-03-19 16:56:45 +00:00
|
|
|
* class MyDirective {
|
2015-03-17 23:29:27 +00:00
|
|
|
* constructor() {
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* ```
|
|
|
|
|
*
|
2015-03-20 21:11:58 +00:00
|
|
|
* This directive would be instantiated with no dependencies.
|
|
|
|
|
*
|
2015-03-17 23:29:27 +00:00
|
|
|
*
|
2015-03-19 16:56:45 +00:00
|
|
|
* ### Component-level injection
|
2015-03-17 23:29:27 +00:00
|
|
|
*
|
2015-05-20 09:48:15 -07:00
|
|
|
* Directives can inject any injectable instance from the closest component injector or any of its
|
|
|
|
|
* parents.
|
2015-03-19 16:56:45 +00:00
|
|
|
*
|
2015-05-20 09:48:15 -07:00
|
|
|
* Here, the constructor declares a parameter, `someService`, and injects the `SomeService` type
|
|
|
|
|
* from the parent
|
2015-03-19 16:56:45 +00:00
|
|
|
* component's injector.
|
2015-03-17 23:29:27 +00:00
|
|
|
* ```
|
2015-04-30 13:38:40 -07:00
|
|
|
* @Directive({ selector: '[my-directive]' })
|
2015-03-19 16:56:45 +00:00
|
|
|
* class MyDirective {
|
|
|
|
|
* constructor(someService: SomeService) {
|
2015-03-17 23:29:27 +00:00
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* ```
|
|
|
|
|
*
|
2015-03-20 21:11:58 +00:00
|
|
|
* This directive would be instantiated with a dependency on `SomeService`.
|
|
|
|
|
*
|
|
|
|
|
*
|
2015-03-19 16:56:45 +00:00
|
|
|
* ### Injecting a directive from the current element
|
2015-03-17 23:29:27 +00:00
|
|
|
*
|
2015-03-19 16:56:45 +00:00
|
|
|
* Directives can inject other directives declared on the current element.
|
2015-03-17 23:29:27 +00:00
|
|
|
*
|
|
|
|
|
* ```
|
2015-04-30 13:38:40 -07:00
|
|
|
* @Directive({ selector: '[my-directive]' })
|
2015-03-19 16:56:45 +00:00
|
|
|
* class MyDirective {
|
|
|
|
|
* constructor(dependency: Dependency) {
|
2015-03-20 13:05:18 -05:00
|
|
|
* expect(dependency.id).toEqual(3);
|
2015-03-17 23:29:27 +00:00
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* ```
|
2015-05-20 09:48:15 -07:00
|
|
|
* This directive would be instantiated with `Dependency` declared at the same element, in this case
|
|
|
|
|
* `dependency="3"`.
|
2015-03-20 21:11:58 +00:00
|
|
|
*
|
2015-03-19 16:56:45 +00:00
|
|
|
* ### Injecting a directive from any ancestor elements
|
2015-03-17 23:29:27 +00:00
|
|
|
*
|
2015-05-20 09:48:15 -07:00
|
|
|
* Directives can inject other directives declared on any ancestor element (in the current Shadow
|
2015-07-29 11:26:09 -07:00
|
|
|
* DOM), i.e. on the current element, the
|
|
|
|
|
* parent element, or its parents.
|
2015-03-17 23:29:27 +00:00
|
|
|
* ```
|
2015-04-30 13:38:40 -07:00
|
|
|
* @Directive({ selector: '[my-directive]' })
|
2015-03-19 16:56:45 +00:00
|
|
|
* class MyDirective {
|
2015-07-29 11:26:09 -07:00
|
|
|
* constructor(@Host() dependency: Dependency) {
|
2015-03-19 16:56:45 +00:00
|
|
|
* expect(dependency.id).toEqual(2);
|
2015-03-17 23:29:27 +00:00
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* ```
|
|
|
|
|
*
|
2015-07-29 11:26:09 -07:00
|
|
|
* `@Host` checks the current element, the parent, as well as its parents recursively. If
|
|
|
|
|
* `dependency="2"` didn't
|
2015-07-13 15:57:06 -07:00
|
|
|
* exist on the direct parent, this injection would
|
2015-05-20 09:48:15 -07:00
|
|
|
* have returned
|
2015-03-20 21:11:58 +00:00
|
|
|
* `dependency="1"`.
|
|
|
|
|
*
|
2015-03-17 23:29:27 +00:00
|
|
|
*
|
2015-04-17 13:01:07 -07:00
|
|
|
* ### Injecting a live collection of direct child directives
|
|
|
|
|
*
|
2015-03-20 21:11:58 +00:00
|
|
|
*
|
2015-05-20 09:48:15 -07:00
|
|
|
* A directive can also query for other child directives. Since parent directives are instantiated
|
2015-06-02 08:55:58 -07:00
|
|
|
* before child directives, a directive can't simply inject the list of child directives. Instead,
|
|
|
|
|
* the directive injects a {@link QueryList}, which updates its contents as children are added,
|
2015-11-23 16:02:19 -08:00
|
|
|
* removed, or moved by a directive that uses a {@link ViewContainerRef} such as a `ngFor`, an
|
|
|
|
|
* `ngIf`, or an `ngSwitch`.
|
2015-03-17 23:29:27 +00:00
|
|
|
*
|
|
|
|
|
* ```
|
2015-04-30 13:38:40 -07:00
|
|
|
* @Directive({ selector: '[my-directive]' })
|
2015-03-19 16:56:45 +00:00
|
|
|
* class MyDirective {
|
2015-05-15 23:02:53 -07:00
|
|
|
* constructor(@Query(Dependency) dependencies:QueryList<Dependency>) {
|
2015-03-17 23:29:27 +00:00
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* ```
|
|
|
|
|
*
|
2015-05-20 09:48:15 -07:00
|
|
|
* This directive would be instantiated with a {@link QueryList} which contains `Dependency` 4 and
|
2015-10-11 20:52:56 +08:00
|
|
|
* `Dependency` 6. Here, `Dependency` 5 would not be included, because it is not a direct child.
|
2015-04-17 13:01:07 -07:00
|
|
|
*
|
|
|
|
|
* ### Injecting a live collection of descendant directives
|
2015-03-17 23:29:27 +00:00
|
|
|
*
|
2015-06-10 17:08:22 -07:00
|
|
|
* By passing the descendant flag to `@Query` above, we can include the children of the child
|
|
|
|
|
* elements.
|
2015-03-17 23:29:27 +00:00
|
|
|
*
|
|
|
|
|
* ```
|
2015-04-30 13:38:40 -07:00
|
|
|
* @Directive({ selector: '[my-directive]' })
|
2015-03-19 16:56:45 +00:00
|
|
|
* class MyDirective {
|
2015-06-10 17:08:22 -07:00
|
|
|
* constructor(@Query(Dependency, {descendants: true}) dependencies:QueryList<Dependency>) {
|
2015-03-17 23:29:27 +00:00
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* ```
|
|
|
|
|
*
|
2015-03-20 21:11:58 +00:00
|
|
|
* This directive would be instantiated with a Query which would contain `Dependency` 4, 5 and 6.
|
2015-03-17 23:29:27 +00:00
|
|
|
*
|
|
|
|
|
* ### Optional injection
|
|
|
|
|
*
|
2015-05-20 09:48:15 -07:00
|
|
|
* The normal behavior of directives is to return an error when a specified dependency cannot be
|
|
|
|
|
* resolved. If you
|
|
|
|
|
* would like to inject `null` on unresolved dependency instead, you can annotate that dependency
|
|
|
|
|
* with `@Optional()`.
|
|
|
|
|
* This explicitly permits the author of a template to treat some of the surrounding directives as
|
|
|
|
|
* optional.
|
2015-03-17 23:29:27 +00:00
|
|
|
*
|
|
|
|
|
* ```
|
2015-04-30 13:38:40 -07:00
|
|
|
* @Directive({ selector: '[my-directive]' })
|
2015-03-19 16:56:45 +00:00
|
|
|
* class MyDirective {
|
2015-03-20 21:11:58 +00:00
|
|
|
* constructor(@Optional() dependency:Dependency) {
|
2015-03-17 23:29:27 +00:00
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* ```
|
|
|
|
|
*
|
2015-05-20 09:48:15 -07:00
|
|
|
* This directive would be instantiated with a `Dependency` directive found on the current element.
|
|
|
|
|
* If none can be
|
2015-03-20 21:11:58 +00:00
|
|
|
* found, the injector supplies `null` instead of throwing an error.
|
|
|
|
|
*
|
2015-10-19 15:37:32 +01:00
|
|
|
* ### Example
|
2015-04-30 13:38:40 -07:00
|
|
|
*
|
|
|
|
|
* Here we use a decorator directive to simply define basic tool-tip behavior.
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
* @Directive({
|
|
|
|
|
* selector: '[tooltip]',
|
2015-09-30 20:59:23 -07:00
|
|
|
* inputs: [
|
2015-05-26 15:54:10 +02:00
|
|
|
* 'text: tooltip'
|
|
|
|
|
* ],
|
2015-08-16 20:37:31 -04:00
|
|
|
* host: {
|
|
|
|
|
* '(mouseenter)': 'onMouseEnter()',
|
|
|
|
|
* '(mouseleave)': 'onMouseLeave()'
|
2015-04-30 13:38:40 -07:00
|
|
|
* }
|
|
|
|
|
* })
|
|
|
|
|
* class Tooltip{
|
|
|
|
|
* text:string;
|
|
|
|
|
* overlay:Overlay; // NOT YET IMPLEMENTED
|
|
|
|
|
* overlayManager:OverlayManager; // NOT YET IMPLEMENTED
|
|
|
|
|
*
|
|
|
|
|
* constructor(overlayManager:OverlayManager) {
|
|
|
|
|
* this.overlay = overlay;
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* onMouseEnter() {
|
|
|
|
|
* // exact signature to be determined
|
|
|
|
|
* this.overlay = this.overlayManager.open(text, ...);
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* onMouseLeave() {
|
|
|
|
|
* this.overlay.close();
|
|
|
|
|
* this.overlay = null;
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* ```
|
2015-05-20 09:48:15 -07:00
|
|
|
* In our HTML template, we can then add this behavior to a `<div>` or any other element with the
|
|
|
|
|
* `tooltip` selector,
|
2015-04-30 13:38:40 -07:00
|
|
|
* like so:
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
* <div tooltip="some text here"></div>
|
|
|
|
|
* ```
|
|
|
|
|
*
|
2015-05-20 09:48:15 -07:00
|
|
|
* Directives can also control the instantiation, destruction, and positioning of inline template
|
|
|
|
|
* elements:
|
2015-04-30 13:38:40 -07:00
|
|
|
*
|
2015-05-20 09:48:15 -07:00
|
|
|
* A directive uses a {@link ViewContainerRef} to instantiate, insert, move, and destroy views at
|
|
|
|
|
* runtime.
|
|
|
|
|
* The {@link ViewContainerRef} is created as a result of `<template>` element, and represents a
|
|
|
|
|
* location in the current view
|
2015-04-30 13:38:40 -07:00
|
|
|
* where these actions are performed.
|
|
|
|
|
*
|
2016-07-28 06:31:26 -07:00
|
|
|
* Views are always created as children of the current {@link ComponentMetadata}, and as siblings of
|
|
|
|
|
* the
|
2015-05-20 09:48:15 -07:00
|
|
|
* `<template>` element. Thus a
|
2015-04-30 13:38:40 -07:00
|
|
|
* directive in a child view cannot inject the directive that created it.
|
|
|
|
|
*
|
2015-05-20 09:48:15 -07:00
|
|
|
* Since directives that create views via ViewContainers are common in Angular, and using the full
|
|
|
|
|
* `<template>` element syntax is wordy, Angular
|
|
|
|
|
* also supports a shorthand notation: `<li *foo="bar">` and `<li template="foo: bar">` are
|
|
|
|
|
* equivalent.
|
2015-04-30 13:38:40 -07:00
|
|
|
*
|
|
|
|
|
* Thus,
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
* <ul>
|
|
|
|
|
* <li *foo="bar" title="text"></li>
|
|
|
|
|
* </ul>
|
|
|
|
|
* ```
|
|
|
|
|
*
|
|
|
|
|
* Expands in use to:
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
* <ul>
|
|
|
|
|
* <template [foo]="bar">
|
|
|
|
|
* <li title="text"></li>
|
|
|
|
|
* </template>
|
|
|
|
|
* </ul>
|
|
|
|
|
* ```
|
|
|
|
|
*
|
2015-05-20 09:48:15 -07:00
|
|
|
* Notice that although the shorthand places `*foo="bar"` within the `<li>` element, the binding for
|
|
|
|
|
* the directive
|
2015-04-30 13:38:40 -07:00
|
|
|
* controller is correctly instantiated on the `<template>` element rather than the `<li>` element.
|
|
|
|
|
*
|
2015-09-02 16:43:39 -07:00
|
|
|
* ## Lifecycle hooks
|
|
|
|
|
*
|
2016-07-07 23:02:35 -07:00
|
|
|
* When the directive class implements some {@linkDocs guide/lifecycle-hooks} the
|
2016-07-06 13:57:38 -07:00
|
|
|
* callbacks are called by the change detection at defined points in time during the life of the
|
|
|
|
|
* directive.
|
2015-04-30 13:38:40 -07:00
|
|
|
*
|
2015-10-19 15:37:32 +01:00
|
|
|
* ### Example
|
2015-04-30 13:38:40 -07:00
|
|
|
*
|
|
|
|
|
* Let's suppose we want to implement the `unless` behavior, to conditionally include a template.
|
|
|
|
|
*
|
|
|
|
|
* Here is a simple directive that triggers on an `unless` selector:
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
* @Directive({
|
|
|
|
|
* selector: '[unless]',
|
2015-09-30 20:59:23 -07:00
|
|
|
* inputs: ['unless']
|
2015-04-30 13:38:40 -07:00
|
|
|
* })
|
|
|
|
|
* export class Unless {
|
|
|
|
|
* viewContainer: ViewContainerRef;
|
2015-07-17 08:03:40 -07:00
|
|
|
* templateRef: TemplateRef;
|
2015-04-30 13:38:40 -07:00
|
|
|
* prevCondition: boolean;
|
|
|
|
|
*
|
2015-07-17 08:03:40 -07:00
|
|
|
* constructor(viewContainer: ViewContainerRef, templateRef: TemplateRef) {
|
2015-04-30 13:38:40 -07:00
|
|
|
* this.viewContainer = viewContainer;
|
2015-07-17 08:03:40 -07:00
|
|
|
* this.templateRef = templateRef;
|
2015-04-30 13:38:40 -07:00
|
|
|
* this.prevCondition = null;
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* set unless(newCondition) {
|
|
|
|
|
* if (newCondition && (isBlank(this.prevCondition) || !this.prevCondition)) {
|
|
|
|
|
* this.prevCondition = true;
|
|
|
|
|
* this.viewContainer.clear();
|
|
|
|
|
* } else if (!newCondition && (isBlank(this.prevCondition) || this.prevCondition)) {
|
|
|
|
|
* this.prevCondition = false;
|
2015-07-17 08:03:40 -07:00
|
|
|
* this.viewContainer.create(this.templateRef);
|
2015-04-30 13:38:40 -07:00
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* ```
|
|
|
|
|
*
|
|
|
|
|
* We can then use this `unless` selector in a template:
|
|
|
|
|
* ```
|
|
|
|
|
* <ul>
|
|
|
|
|
* <li *unless="expr"></li>
|
|
|
|
|
* </ul>
|
|
|
|
|
* ```
|
|
|
|
|
*
|
2015-05-20 09:48:15 -07:00
|
|
|
* Once the directive instantiates the child view, the shorthand notation for the template expands
|
|
|
|
|
* and the result is:
|
2015-04-30 13:38:40 -07:00
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
* <ul>
|
|
|
|
|
* <template [unless]="exp">
|
|
|
|
|
* <li></li>
|
|
|
|
|
* </template>
|
|
|
|
|
* <li></li>
|
|
|
|
|
* </ul>
|
|
|
|
|
* ```
|
|
|
|
|
*
|
2015-05-20 09:48:15 -07:00
|
|
|
* Note also that although the `<li></li>` template still exists inside the `<template></template>`,
|
|
|
|
|
* the instantiated
|
2015-04-30 13:38:40 -07:00
|
|
|
* view occurs on the second `<li></li>` which is a sibling to the `<template>` element.
|
2016-05-25 15:00:05 -07:00
|
|
|
* @stable
|
2015-03-13 21:54:19 +00:00
|
|
|
*/
|
2016-07-29 02:10:30 -07:00
|
|
|
export class DirectiveMetadata extends InjectableMetadata implements DirectiveMetadataType {
|
2015-03-14 00:00:42 +00:00
|
|
|
/**
|
2015-03-17 19:22:13 +00:00
|
|
|
* The CSS selector that triggers the instantiation of a directive.
|
|
|
|
|
*
|
2015-05-20 09:48:15 -07:00
|
|
|
* Angular only allows directives to trigger on CSS selectors that do not cross element
|
|
|
|
|
* boundaries.
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-03-20 21:11:58 +00:00
|
|
|
* `selector` may be declared as one of the following:
|
|
|
|
|
*
|
|
|
|
|
* - `element-name`: select by element name.
|
|
|
|
|
* - `.class`: select by class name.
|
|
|
|
|
* - `[attribute]`: select by attribute name.
|
|
|
|
|
* - `[attribute=value]`: select by attribute name and value.
|
|
|
|
|
* - `:not(sub_selector)`: select only if the element does not match the `sub_selector`.
|
2015-03-19 17:01:42 +01:00
|
|
|
* - `selector1, selector2`: select if either `selector1` or `selector2` matches.
|
2015-03-20 21:11:58 +00:00
|
|
|
*
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-10-19 15:37:32 +01:00
|
|
|
* ### Example
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-03-14 00:00:42 +00:00
|
|
|
* Suppose we have a directive with an `input[type=text]` selector.
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-03-14 00:00:42 +00:00
|
|
|
* And the following HTML:
|
|
|
|
|
*
|
2015-03-17 19:22:13 +00:00
|
|
|
* ```html
|
|
|
|
|
* <form>
|
|
|
|
|
* <input type="text">
|
|
|
|
|
* <input type="radio">
|
|
|
|
|
* <form>
|
|
|
|
|
* ```
|
|
|
|
|
*
|
2015-03-14 00:00:42 +00:00
|
|
|
* The directive would only be instantiated on the `<input type="text">` element.
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-03-14 00:00:42 +00:00
|
|
|
*/
|
2015-07-07 22:09:19 +02:00
|
|
|
selector: string;
|
2015-03-17 19:22:13 +00:00
|
|
|
|
2015-03-14 00:00:42 +00:00
|
|
|
/**
|
2015-09-30 20:59:23 -07:00
|
|
|
* Enumerates the set of data-bound input properties for a directive
|
2015-09-24 13:46:13 -07:00
|
|
|
*
|
2015-09-30 20:59:23 -07:00
|
|
|
* Angular automatically updates input properties during change detection.
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-09-30 20:59:23 -07:00
|
|
|
* The `inputs` property defines a set of `directiveProperty` to `bindingProperty`
|
2015-05-26 15:54:10 +02:00
|
|
|
* configuration:
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-03-14 00:00:42 +00:00
|
|
|
* - `directiveProperty` specifies the component property where the value is written.
|
2015-03-17 19:22:13 +00:00
|
|
|
* - `bindingProperty` specifies the DOM property where the value is read from.
|
|
|
|
|
*
|
2015-09-24 13:46:13 -07:00
|
|
|
* When `bindingProperty` is not provided, it is assumed to be equal to `directiveProperty`.
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-09-24 13:46:13 -07:00
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/ivhfXY?p=preview))
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-09-24 13:46:13 -07:00
|
|
|
* The following example creates a component with two data-bound properties.
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-09-24 13:46:13 -07:00
|
|
|
* ```typescript
|
|
|
|
|
* @Component({
|
|
|
|
|
* selector: 'bank-account',
|
2015-10-11 07:41:19 -07:00
|
|
|
* inputs: ['bankName', 'id: account-id'],
|
2015-09-24 13:46:13 -07:00
|
|
|
* template: `
|
|
|
|
|
* Bank Name: {{bankName}}
|
|
|
|
|
* Account Id: {{id}}
|
|
|
|
|
* `
|
|
|
|
|
* })
|
|
|
|
|
* class BankAccount {
|
|
|
|
|
* bankName: string;
|
|
|
|
|
* id: string;
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-09-24 13:46:13 -07:00
|
|
|
* // this property is not bound, and won't be automatically updated by Angular
|
|
|
|
|
* normalizedBankName: string;
|
|
|
|
|
* }
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-10-11 07:41:19 -07:00
|
|
|
* @Component({
|
|
|
|
|
* selector: 'app',
|
2015-09-24 13:46:13 -07:00
|
|
|
* template: `
|
|
|
|
|
* <bank-account bank-name="RBC" account-id="4747"></bank-account>
|
|
|
|
|
* `,
|
|
|
|
|
* directives: [BankAccount]
|
|
|
|
|
* })
|
|
|
|
|
* class App {}
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-09-24 13:46:13 -07:00
|
|
|
* bootstrap(App);
|
2015-03-17 19:22:13 +00:00
|
|
|
* ```
|
|
|
|
|
*
|
2015-03-14 00:00:42 +00:00
|
|
|
*/
|
2015-10-10 22:11:13 -07:00
|
|
|
get inputs(): string[] {
|
|
|
|
|
return isPresent(this._properties) && this._properties.length > 0 ? this._properties :
|
|
|
|
|
this._inputs;
|
|
|
|
|
}
|
2016-02-14 01:31:50 +02:00
|
|
|
/**
|
|
|
|
|
* Use `inputs` instead
|
|
|
|
|
*
|
|
|
|
|
* @deprecated
|
|
|
|
|
*/
|
2015-10-10 22:11:13 -07:00
|
|
|
get properties(): string[] { return this.inputs; }
|
|
|
|
|
private _inputs: string[];
|
|
|
|
|
private _properties: string[];
|
2015-10-02 16:21:49 -07:00
|
|
|
|
2015-04-14 14:34:41 -07:00
|
|
|
/**
|
2015-09-30 20:59:23 -07:00
|
|
|
* Enumerates the set of event-bound output properties.
|
2015-04-14 14:34:41 -07:00
|
|
|
*
|
2015-09-30 20:59:23 -07:00
|
|
|
* When an output property emits an event, an event handler attached to that event
|
2015-09-24 13:46:13 -07:00
|
|
|
* the template is invoked.
|
2015-04-14 14:34:41 -07:00
|
|
|
*
|
2015-09-30 20:59:23 -07:00
|
|
|
* The `outputs` property defines a set of `directiveProperty` to `bindingProperty`
|
2015-09-24 13:46:13 -07:00
|
|
|
* configuration:
|
2015-04-14 14:34:41 -07:00
|
|
|
*
|
2015-09-24 13:46:13 -07:00
|
|
|
* - `directiveProperty` specifies the component property that emits events.
|
|
|
|
|
* - `bindingProperty` specifies the DOM property the event handler is attached to.
|
2015-06-04 15:09:54 +02:00
|
|
|
*
|
2015-09-24 13:46:13 -07:00
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/d5CNq7?p=preview))
|
2015-06-04 15:09:54 +02:00
|
|
|
*
|
2015-09-24 13:46:13 -07:00
|
|
|
* ```typescript
|
|
|
|
|
* @Directive({
|
|
|
|
|
* selector: 'interval-dir',
|
2015-09-30 20:59:23 -07:00
|
|
|
* outputs: ['everySecond', 'five5Secs: everyFiveSeconds']
|
2015-06-04 15:09:54 +02:00
|
|
|
* })
|
2015-09-24 13:46:13 -07:00
|
|
|
* class IntervalDir {
|
|
|
|
|
* everySecond = new EventEmitter();
|
|
|
|
|
* five5Secs = new EventEmitter();
|
2015-06-04 15:09:54 +02:00
|
|
|
*
|
|
|
|
|
* constructor() {
|
2015-11-15 23:58:59 -08:00
|
|
|
* setInterval(() => this.everySecond.emit("event"), 1000);
|
|
|
|
|
* setInterval(() => this.five5Secs.emit("event"), 5000);
|
2015-06-04 15:09:54 +02:00
|
|
|
* }
|
2015-09-24 13:46:13 -07:00
|
|
|
* }
|
2015-06-04 15:09:54 +02:00
|
|
|
*
|
2015-10-11 07:41:19 -07:00
|
|
|
* @Component({
|
|
|
|
|
* selector: 'app',
|
2015-09-24 13:46:13 -07:00
|
|
|
* template: `
|
2016-01-22 23:10:48 -08:00
|
|
|
* <interval-dir (everySecond)="everySecond()" (everyFiveSeconds)="everyFiveSeconds()">
|
2015-09-24 13:46:13 -07:00
|
|
|
* </interval-dir>
|
|
|
|
|
* `,
|
|
|
|
|
* directives: [IntervalDir]
|
|
|
|
|
* })
|
|
|
|
|
* class App {
|
|
|
|
|
* everySecond() { console.log('second'); }
|
|
|
|
|
* everyFiveSeconds() { console.log('five seconds'); }
|
2015-06-04 15:09:54 +02:00
|
|
|
* }
|
2015-09-24 13:46:13 -07:00
|
|
|
* bootstrap(App);
|
2015-06-04 15:09:54 +02:00
|
|
|
* ```
|
|
|
|
|
*
|
2015-04-14 14:34:41 -07:00
|
|
|
*/
|
2015-10-10 22:11:13 -07:00
|
|
|
get outputs(): string[] {
|
|
|
|
|
return isPresent(this._events) && this._events.length > 0 ? this._events : this._outputs;
|
|
|
|
|
}
|
2016-02-14 01:31:50 +02:00
|
|
|
/**
|
|
|
|
|
* Use `outputs` instead
|
|
|
|
|
*
|
|
|
|
|
* @deprecated
|
|
|
|
|
*/
|
2015-10-10 22:11:13 -07:00
|
|
|
get events(): string[] { return this.outputs; }
|
|
|
|
|
private _outputs: string[];
|
|
|
|
|
private _events: string[];
|
2015-10-02 16:21:49 -07:00
|
|
|
|
2015-03-14 00:00:42 +00:00
|
|
|
/**
|
2015-09-13 14:30:55 +02:00
|
|
|
* Specify the events, actions, properties and attributes related to the host element.
|
2015-06-09 12:46:21 +02:00
|
|
|
*
|
2015-10-01 15:47:51 -07:00
|
|
|
* ## Host Listeners
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-10-01 15:47:51 -07:00
|
|
|
* Specifies which DOM events a directive listens to via a set of `(event)` to `method`
|
2015-06-09 12:46:21 +02:00
|
|
|
* key-value pairs:
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-11-30 16:39:24 +01:00
|
|
|
* - `event`: the DOM event that the directive listens to.
|
2015-04-03 22:07:15 +02:00
|
|
|
* - `statement`: the statement to execute when the event occurs.
|
2015-09-11 22:02:13 -07:00
|
|
|
* If the evaluation of the statement returns `false`, then `preventDefault`is applied on the DOM
|
2015-05-20 09:48:15 -07:00
|
|
|
* event.
|
2015-03-20 21:11:58 +00:00
|
|
|
*
|
2015-04-02 15:56:58 +02:00
|
|
|
* To listen to global events, a target must be added to the event name.
|
|
|
|
|
* The target can be `window`, `document` or `body`.
|
2015-03-20 21:11:58 +00:00
|
|
|
*
|
2015-10-01 15:47:51 -07:00
|
|
|
* When writing a directive event binding, you can also refer to the $event local variable.
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-10-01 15:47:51 -07:00
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/DlA5KU?p=preview))
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-10-01 15:47:51 -07:00
|
|
|
* The following example declares a directive that attaches a click listener to the button and
|
|
|
|
|
* counts clicks.
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-10-01 15:47:51 -07:00
|
|
|
* ```typescript
|
2015-04-30 13:38:40 -07:00
|
|
|
* @Directive({
|
2015-10-01 15:47:51 -07:00
|
|
|
* selector: 'button[counting]',
|
2015-06-09 12:46:21 +02:00
|
|
|
* host: {
|
2015-10-01 15:47:51 -07:00
|
|
|
* '(click)': 'onClick($event.target)'
|
2015-03-17 19:22:13 +00:00
|
|
|
* }
|
|
|
|
|
* })
|
2015-10-01 15:47:51 -07:00
|
|
|
* class CountClicks {
|
|
|
|
|
* numberOfClicks = 0;
|
|
|
|
|
*
|
|
|
|
|
* onClick(btn) {
|
|
|
|
|
* console.log("button", btn, "number of clicks:", this.numberOfClicks++);
|
2015-04-02 15:56:58 +02:00
|
|
|
* }
|
2015-03-17 19:22:13 +00:00
|
|
|
* }
|
2015-10-01 15:47:51 -07:00
|
|
|
*
|
2015-10-11 07:41:19 -07:00
|
|
|
* @Component({
|
|
|
|
|
* selector: 'app',
|
2015-10-01 15:47:51 -07:00
|
|
|
* template: `<button counting>Increment</button>`,
|
|
|
|
|
* directives: [CountClicks]
|
|
|
|
|
* })
|
|
|
|
|
* class App {}
|
|
|
|
|
*
|
|
|
|
|
* bootstrap(App);
|
2015-03-17 19:22:13 +00:00
|
|
|
* ```
|
|
|
|
|
*
|
2015-10-01 15:47:51 -07:00
|
|
|
* ## Host Property Bindings
|
2015-03-20 21:11:58 +00:00
|
|
|
*
|
2015-10-01 15:47:51 -07:00
|
|
|
* Specifies which DOM properties a directive updates.
|
2015-04-21 11:47:53 -07:00
|
|
|
*
|
2015-10-01 15:47:51 -07:00
|
|
|
* Angular automatically checks host property bindings during change detection.
|
|
|
|
|
* If a binding changes, it will update the host element of the directive.
|
2015-04-21 11:47:53 -07:00
|
|
|
*
|
2015-10-01 15:47:51 -07:00
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/gNg0ED?p=preview))
|
|
|
|
|
*
|
|
|
|
|
* The following example creates a directive that sets the `valid` and `invalid` classes
|
2015-11-23 16:02:19 -08:00
|
|
|
* on the DOM element that has ngModel directive on it.
|
2015-10-01 15:47:51 -07:00
|
|
|
*
|
|
|
|
|
* ```typescript
|
2015-04-30 13:38:40 -07:00
|
|
|
* @Directive({
|
2015-11-23 16:02:19 -08:00
|
|
|
* selector: '[ngModel]',
|
2015-06-09 12:46:21 +02:00
|
|
|
* host: {
|
2015-10-01 15:47:51 -07:00
|
|
|
* '[class.valid]': 'valid',
|
|
|
|
|
* '[class.invalid]': 'invalid'
|
2015-04-21 11:47:53 -07:00
|
|
|
* }
|
|
|
|
|
* })
|
2015-10-01 15:47:51 -07:00
|
|
|
* class NgModelStatus {
|
|
|
|
|
* constructor(public control:NgModel) {}
|
|
|
|
|
* get valid { return this.control.valid; }
|
|
|
|
|
* get invalid { return this.control.invalid; }
|
|
|
|
|
* }
|
|
|
|
|
*
|
2015-10-11 07:41:19 -07:00
|
|
|
* @Component({
|
|
|
|
|
* selector: 'app',
|
2015-11-23 16:02:19 -08:00
|
|
|
* template: `<input [(ngModel)]="prop">`,
|
2015-10-01 15:47:51 -07:00
|
|
|
* directives: [FORM_DIRECTIVES, NgModelStatus]
|
|
|
|
|
* })
|
|
|
|
|
* class App {
|
|
|
|
|
* prop;
|
2015-04-21 11:47:53 -07:00
|
|
|
* }
|
2015-06-09 12:46:21 +02:00
|
|
|
*
|
2015-10-01 15:47:51 -07:00
|
|
|
* bootstrap(App);
|
|
|
|
|
* ```
|
2015-06-09 12:46:21 +02:00
|
|
|
*
|
|
|
|
|
* ## Attributes
|
|
|
|
|
*
|
2015-10-01 15:47:51 -07:00
|
|
|
* Specifies static attributes that should be propagated to a host element.
|
2015-05-01 13:41:56 +02:00
|
|
|
*
|
2015-10-01 15:47:51 -07:00
|
|
|
* ### Example
|
2015-05-01 13:41:56 +02:00
|
|
|
*
|
2015-10-01 15:47:51 -07:00
|
|
|
* In this example using `my-button` directive (ex.: `<div my-button></div>`) on a host element
|
|
|
|
|
* (here: `<div>` ) will ensure that this element will get the "button" role.
|
|
|
|
|
*
|
|
|
|
|
* ```typescript
|
2015-05-01 13:41:56 +02:00
|
|
|
* @Directive({
|
|
|
|
|
* selector: '[my-button]',
|
2015-06-09 12:46:21 +02:00
|
|
|
* host: {
|
2015-05-01 13:41:56 +02:00
|
|
|
* 'role': 'button'
|
|
|
|
|
* }
|
|
|
|
|
* })
|
|
|
|
|
* class MyButton {
|
|
|
|
|
* }
|
2015-06-09 12:46:21 +02:00
|
|
|
* ```
|
2015-05-11 12:31:16 -07:00
|
|
|
*/
|
2015-10-02 16:47:54 -07:00
|
|
|
host: {[key: string]: string};
|
2015-05-11 12:31:16 -07:00
|
|
|
|
2015-05-16 11:01:02 -07:00
|
|
|
/**
|
2015-09-13 14:30:55 +02:00
|
|
|
* Defines the set of injectable objects that are visible to a Directive and its light DOM
|
2015-05-20 09:48:15 -07:00
|
|
|
* children.
|
2015-05-16 11:01:02 -07:00
|
|
|
*
|
|
|
|
|
* ## Simple Example
|
|
|
|
|
*
|
|
|
|
|
* Here is an example of a class that can be injected:
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
* class Greeter {
|
|
|
|
|
* greet(name:string) {
|
|
|
|
|
* return 'Hello ' + name + '!';
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* @Directive({
|
|
|
|
|
* selector: 'greet',
|
2016-03-21 16:27:17 +05:30
|
|
|
* providers: [
|
2015-05-16 11:01:02 -07:00
|
|
|
* Greeter
|
|
|
|
|
* ]
|
|
|
|
|
* })
|
|
|
|
|
* class HelloWorld {
|
|
|
|
|
* greeter:Greeter;
|
|
|
|
|
*
|
|
|
|
|
* constructor(greeter:Greeter) {
|
|
|
|
|
* this.greeter = greeter;
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
2016-06-08 16:38:52 -07:00
|
|
|
get providers(): any[] { return this._providers; }
|
2015-10-10 22:11:13 -07:00
|
|
|
private _providers: any[];
|
2015-05-16 11:01:02 -07:00
|
|
|
|
2015-06-04 13:45:08 -07:00
|
|
|
/**
|
|
|
|
|
* Defines the name that can be used in the template to assign this directive to a variable.
|
|
|
|
|
*
|
|
|
|
|
* ## Simple Example
|
|
|
|
|
*
|
2015-06-09 23:11:08 +01:00
|
|
|
* ```
|
2015-06-04 13:45:08 -07:00
|
|
|
* @Directive({
|
|
|
|
|
* selector: 'child-dir',
|
|
|
|
|
* exportAs: 'child'
|
|
|
|
|
* })
|
|
|
|
|
* class ChildDir {
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* @Component({
|
|
|
|
|
* selector: 'main',
|
|
|
|
|
* template: `<child-dir #c="child"></child-dir>`,
|
|
|
|
|
* directives: [ChildDir]
|
|
|
|
|
* })
|
|
|
|
|
* class MainComponent {
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
2015-07-07 22:09:19 +02:00
|
|
|
exportAs: string;
|
2015-07-06 16:32:29 -07:00
|
|
|
|
2015-09-17 18:45:14 -07:00
|
|
|
// TODO: add an example after ContentChildren and ViewChildren are in master
|
|
|
|
|
/**
|
|
|
|
|
* Configures the queries that will be injected into the directive.
|
|
|
|
|
*
|
refactor(lifecycle): prefix lifecycle methods with "ng"
BREAKING CHANGE:
Previously, components that would implement lifecycle interfaces would include methods
like "onChanges" or "afterViewInit." Given that components were at risk of using such
names without realizing that Angular would call the methods at different points of
the component lifecycle. This change adds an "ng" prefix to all lifecycle hook methods,
far reducing the risk of an accidental name collision.
To fix, just rename these methods:
* onInit
* onDestroy
* doCheck
* onChanges
* afterContentInit
* afterContentChecked
* afterViewInit
* afterViewChecked
* _Router Hooks_
* onActivate
* onReuse
* onDeactivate
* canReuse
* canDeactivate
To:
* ngOnInit,
* ngOnDestroy,
* ngDoCheck,
* ngOnChanges,
* ngAfterContentInit,
* ngAfterContentChecked,
* ngAfterViewInit,
* ngAfterViewChecked
* _Router Hooks_
* routerOnActivate
* routerOnReuse
* routerOnDeactivate
* routerCanReuse
* routerCanDeactivate
The names of lifecycle interfaces and enums have not changed, though interfaces
have been updated to reflect the new method names.
Closes #5036
2015-11-16 17:04:36 -08:00
|
|
|
* Content queries are set before the `ngAfterContentInit` callback is called.
|
|
|
|
|
* View queries are set before the `ngAfterViewInit` callback is called.
|
2015-09-17 18:45:14 -07:00
|
|
|
*
|
|
|
|
|
* ### Example
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
* @Component({
|
|
|
|
|
* selector: 'someDir',
|
|
|
|
|
* queries: {
|
|
|
|
|
* contentChildren: new ContentChildren(ChildDirective),
|
|
|
|
|
* viewChildren: new ViewChildren(ChildDirective)
|
2015-10-11 07:41:19 -07:00
|
|
|
* },
|
2015-09-17 18:45:14 -07:00
|
|
|
* template: '<child-directive></child-directive>',
|
|
|
|
|
* directives: [ChildDirective]
|
|
|
|
|
* })
|
|
|
|
|
* class SomeDir {
|
|
|
|
|
* contentChildren: QueryList<ChildDirective>,
|
|
|
|
|
* viewChildren: QueryList<ChildDirective>
|
|
|
|
|
*
|
refactor(lifecycle): prefix lifecycle methods with "ng"
BREAKING CHANGE:
Previously, components that would implement lifecycle interfaces would include methods
like "onChanges" or "afterViewInit." Given that components were at risk of using such
names without realizing that Angular would call the methods at different points of
the component lifecycle. This change adds an "ng" prefix to all lifecycle hook methods,
far reducing the risk of an accidental name collision.
To fix, just rename these methods:
* onInit
* onDestroy
* doCheck
* onChanges
* afterContentInit
* afterContentChecked
* afterViewInit
* afterViewChecked
* _Router Hooks_
* onActivate
* onReuse
* onDeactivate
* canReuse
* canDeactivate
To:
* ngOnInit,
* ngOnDestroy,
* ngDoCheck,
* ngOnChanges,
* ngAfterContentInit,
* ngAfterContentChecked,
* ngAfterViewInit,
* ngAfterViewChecked
* _Router Hooks_
* routerOnActivate
* routerOnReuse
* routerOnDeactivate
* routerCanReuse
* routerCanDeactivate
The names of lifecycle interfaces and enums have not changed, though interfaces
have been updated to reflect the new method names.
Closes #5036
2015-11-16 17:04:36 -08:00
|
|
|
* ngAfterContentInit() {
|
2015-09-17 18:45:14 -07:00
|
|
|
* // contentChildren is set
|
|
|
|
|
* }
|
|
|
|
|
*
|
refactor(lifecycle): prefix lifecycle methods with "ng"
BREAKING CHANGE:
Previously, components that would implement lifecycle interfaces would include methods
like "onChanges" or "afterViewInit." Given that components were at risk of using such
names without realizing that Angular would call the methods at different points of
the component lifecycle. This change adds an "ng" prefix to all lifecycle hook methods,
far reducing the risk of an accidental name collision.
To fix, just rename these methods:
* onInit
* onDestroy
* doCheck
* onChanges
* afterContentInit
* afterContentChecked
* afterViewInit
* afterViewChecked
* _Router Hooks_
* onActivate
* onReuse
* onDeactivate
* canReuse
* canDeactivate
To:
* ngOnInit,
* ngOnDestroy,
* ngDoCheck,
* ngOnChanges,
* ngAfterContentInit,
* ngAfterContentChecked,
* ngAfterViewInit,
* ngAfterViewChecked
* _Router Hooks_
* routerOnActivate
* routerOnReuse
* routerOnDeactivate
* routerCanReuse
* routerCanDeactivate
The names of lifecycle interfaces and enums have not changed, though interfaces
have been updated to reflect the new method names.
Closes #5036
2015-11-16 17:04:36 -08:00
|
|
|
* ngAfterViewInit() {
|
2015-09-17 18:45:14 -07:00
|
|
|
* // viewChildren is set
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
2015-10-02 16:47:54 -07:00
|
|
|
queries: {[key: string]: any};
|
2015-09-17 18:45:14 -07:00
|
|
|
|
2016-07-29 02:10:30 -07:00
|
|
|
constructor(
|
|
|
|
|
{selector, inputs, outputs, properties, events, host, providers, exportAs,
|
|
|
|
|
queries}: DirectiveMetadataType = {}) {
|
2015-07-13 15:48:28 -07:00
|
|
|
super();
|
2015-07-07 22:09:19 +02:00
|
|
|
this.selector = selector;
|
2015-10-10 22:11:13 -07:00
|
|
|
this._inputs = inputs;
|
|
|
|
|
this._properties = properties;
|
|
|
|
|
this._outputs = outputs;
|
|
|
|
|
this._events = events;
|
2015-07-07 22:09:19 +02:00
|
|
|
this.host = host;
|
|
|
|
|
this.exportAs = exportAs;
|
2015-09-17 18:45:14 -07:00
|
|
|
this.queries = queries;
|
2015-10-10 22:11:13 -07:00
|
|
|
this._providers = providers;
|
2015-07-07 22:09:19 +02:00
|
|
|
}
|
2015-06-12 23:51:42 -07:00
|
|
|
}
|
|
|
|
|
|
2016-07-29 02:10:30 -07:00
|
|
|
/**
|
|
|
|
|
* Interface for creating {@link ComponentMetadataType}
|
|
|
|
|
* @experimental
|
|
|
|
|
*/
|
|
|
|
|
export interface ComponentMetadataType extends DirectiveMetadataType {
|
|
|
|
|
changeDetection?: ChangeDetectionStrategy;
|
|
|
|
|
viewProviders?: any[];
|
|
|
|
|
moduleId?: string;
|
|
|
|
|
templateUrl?: string;
|
|
|
|
|
template?: string;
|
|
|
|
|
styleUrls?: string[];
|
|
|
|
|
styles?: string[];
|
|
|
|
|
animations?: AnimationEntryMetadata[];
|
|
|
|
|
directives?: Array<Type|any[]>;
|
|
|
|
|
pipes?: Array<Type|any[]>;
|
|
|
|
|
encapsulation?: ViewEncapsulation;
|
|
|
|
|
interpolation?: [string, string];
|
|
|
|
|
entryComponents?: Array<Type|any[]>;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-17 19:22:13 +00:00
|
|
|
/**
|
2015-03-30 17:19:27 -07:00
|
|
|
* Declare reusable UI building blocks for an application.
|
2015-03-17 23:29:27 +00:00
|
|
|
*
|
2015-11-20 16:02:29 -08:00
|
|
|
* Each Angular component requires a single `@Component` annotation. The
|
2015-05-20 09:48:15 -07:00
|
|
|
* `@Component`
|
|
|
|
|
* annotation specifies when a component is instantiated, and which properties and hostListeners it
|
|
|
|
|
* binds to.
|
2015-03-17 23:29:27 +00:00
|
|
|
*
|
2015-03-20 21:11:58 +00:00
|
|
|
* When a component is instantiated, Angular
|
|
|
|
|
* - creates a shadow DOM for the component.
|
|
|
|
|
* - loads the selected template into the shadow DOM.
|
2015-10-10 22:11:13 -07:00
|
|
|
* - creates all the injectable objects configured with `providers` and `viewProviders`.
|
2015-03-20 21:11:58 +00:00
|
|
|
*
|
2015-03-30 17:19:27 -07:00
|
|
|
* All template expressions and statements are then evaluated against the component instance.
|
2015-03-20 21:11:58 +00:00
|
|
|
*
|
2015-09-02 16:43:39 -07:00
|
|
|
* ## Lifecycle hooks
|
|
|
|
|
*
|
2016-07-07 23:02:35 -07:00
|
|
|
* When the component class implements some {@linkDocs guide/lifecycle-hooks} the
|
2016-07-06 13:57:38 -07:00
|
|
|
* callbacks are called by the change detection at defined points in time during the life of the
|
|
|
|
|
* component.
|
2015-09-02 16:43:39 -07:00
|
|
|
*
|
2015-10-19 15:37:32 +01:00
|
|
|
* ### Example
|
2015-03-20 21:11:58 +00:00
|
|
|
*
|
2015-11-30 08:28:54 -08:00
|
|
|
* {@example core/ts/metadata/metadata.ts region='component'}
|
2016-05-25 15:00:05 -07:00
|
|
|
* @stable
|
2015-03-17 19:22:13 +00:00
|
|
|
*/
|
2016-07-29 02:10:30 -07:00
|
|
|
export class ComponentMetadata extends DirectiveMetadata implements ComponentMetadataType {
|
2015-07-07 22:09:19 +02:00
|
|
|
/**
|
|
|
|
|
* Defines the used change detection strategy.
|
|
|
|
|
*
|
|
|
|
|
* When a component is instantiated, Angular creates a change detector, which is responsible for
|
2015-08-26 11:44:59 -07:00
|
|
|
* propagating the component's bindings.
|
2015-07-07 22:09:19 +02:00
|
|
|
*
|
|
|
|
|
* The `changeDetection` property defines, whether the change detection will be checked every time
|
2015-08-26 11:44:59 -07:00
|
|
|
* or only when the component tells it to do so.
|
2015-07-07 22:09:19 +02:00
|
|
|
*/
|
2015-08-26 11:44:59 -07:00
|
|
|
changeDetection: ChangeDetectionStrategy;
|
2015-03-30 16:54:10 -07:00
|
|
|
|
2015-05-16 11:01:02 -07:00
|
|
|
/**
|
2015-09-13 14:30:55 +02:00
|
|
|
* Defines the set of injectable objects that are visible to its view DOM children.
|
2015-05-16 11:01:02 -07:00
|
|
|
*
|
|
|
|
|
* ## Simple Example
|
|
|
|
|
*
|
|
|
|
|
* Here is an example of a class that can be injected:
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
* class Greeter {
|
|
|
|
|
* greet(name:string) {
|
|
|
|
|
* return 'Hello ' + name + '!';
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* @Directive({
|
|
|
|
|
* selector: 'needs-greeter'
|
|
|
|
|
* })
|
|
|
|
|
* class NeedsGreeter {
|
|
|
|
|
* greeter:Greeter;
|
|
|
|
|
*
|
|
|
|
|
* constructor(greeter:Greeter) {
|
|
|
|
|
* this.greeter = greeter;
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* @Component({
|
|
|
|
|
* selector: 'greet',
|
2015-10-10 22:11:13 -07:00
|
|
|
* viewProviders: [
|
2015-05-16 11:01:02 -07:00
|
|
|
* Greeter
|
2015-10-11 07:41:19 -07:00
|
|
|
* ],
|
2015-05-16 11:01:02 -07:00
|
|
|
* template: `<needs-greeter></needs-greeter>`,
|
|
|
|
|
* directives: [NeedsGreeter]
|
|
|
|
|
* })
|
|
|
|
|
* class HelloWorld {
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
2016-06-08 16:38:52 -07:00
|
|
|
get viewProviders(): any[] { return this._viewProviders; }
|
2015-10-10 22:11:13 -07:00
|
|
|
private _viewProviders: any[];
|
2015-07-06 16:32:29 -07:00
|
|
|
|
2015-12-13 17:35:33 -08:00
|
|
|
/**
|
|
|
|
|
* The module id of the module that contains the component.
|
|
|
|
|
* Needed to be able to resolve relative urls for templates and styles.
|
2016-05-27 12:22:07 -07:00
|
|
|
* In CommonJS, this can always be set to `module.id`, similarly SystemJS exposes `__moduleName`
|
|
|
|
|
* variable within each module.
|
|
|
|
|
*
|
2015-12-13 17:35:33 -08:00
|
|
|
*
|
|
|
|
|
* ## Simple Example
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
* @Directive({
|
|
|
|
|
* selector: 'someDir',
|
|
|
|
|
* moduleId: module.id
|
|
|
|
|
* })
|
|
|
|
|
* class SomeDir {
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
|
|
|
|
moduleId: string;
|
|
|
|
|
|
2016-07-28 06:31:26 -07:00
|
|
|
/**
|
|
|
|
|
* Specifies a template URL for an Angular component.
|
|
|
|
|
*
|
|
|
|
|
* NOTE: Only one of `templateUrl` or `template` can be defined per View.
|
|
|
|
|
*
|
|
|
|
|
* <!-- TODO: what's the url relative to? -->
|
|
|
|
|
*/
|
2015-10-06 17:03:37 -07:00
|
|
|
templateUrl: string;
|
|
|
|
|
|
2016-07-28 06:31:26 -07:00
|
|
|
/**
|
|
|
|
|
* Specifies an inline template for an Angular component.
|
|
|
|
|
*
|
|
|
|
|
* NOTE: Only one of `templateUrl` or `template` can be defined per View.
|
|
|
|
|
*/
|
2015-10-06 17:03:37 -07:00
|
|
|
template: string;
|
|
|
|
|
|
2016-07-28 06:31:26 -07:00
|
|
|
/**
|
|
|
|
|
* Specifies stylesheet URLs for an Angular component.
|
|
|
|
|
*
|
|
|
|
|
* <!-- TODO: what's the url relative to? -->
|
|
|
|
|
*/
|
2015-10-06 17:03:37 -07:00
|
|
|
styleUrls: string[];
|
|
|
|
|
|
2016-07-28 06:31:26 -07:00
|
|
|
/**
|
|
|
|
|
* Specifies an inline stylesheet for an Angular component.
|
|
|
|
|
*/
|
2015-10-06 17:03:37 -07:00
|
|
|
styles: string[];
|
|
|
|
|
|
2016-05-26 09:22:44 -07:00
|
|
|
/**
|
|
|
|
|
* Animations are defined on components via an animation-like DSL. This DSL approach to describing
|
|
|
|
|
* animations allows for a flexibility that both benefits developers and the framework.
|
|
|
|
|
*
|
|
|
|
|
* Animations work by listening on state changes that occur on an element within
|
|
|
|
|
* the template. When a state change occurs, Angular can then take advantage and animate the
|
|
|
|
|
* arc in between. This works similar to how CSS transitions work, however, by having a
|
|
|
|
|
* programmatic DSL, animations are not limited to environments that are DOM-specific.
|
|
|
|
|
* (Angular can also perform optimizations behind the scenes to make animations more performant.)
|
|
|
|
|
*
|
|
|
|
|
* For animations to be available for use, animation state changes are placed within
|
|
|
|
|
* {@link trigger animation triggers} which are housed inside of the `animations` annotation
|
|
|
|
|
* metadata. Within a trigger both {@link state state} and {@link transition transition} entries
|
|
|
|
|
* can be placed.
|
|
|
|
|
*
|
|
|
|
|
* ```typescript
|
|
|
|
|
* @Component({
|
|
|
|
|
* selector: 'animation-cmp',
|
|
|
|
|
* templateUrl: 'animation-cmp.html',
|
|
|
|
|
* animations: [
|
|
|
|
|
* // this here is our animation trigger that
|
|
|
|
|
* // will contain our state change animations.
|
|
|
|
|
* trigger('myTriggerName', [
|
|
|
|
|
* // the styles defined for the `on` and `off`
|
|
|
|
|
* // states declared below are persisted on the
|
|
|
|
|
* // element once the animation completes.
|
|
|
|
|
* state('on', style({ opacity: 1 }),
|
|
|
|
|
* state('off', style({ opacity: 0 }),
|
|
|
|
|
*
|
|
|
|
|
* // this here is our animation that kicks off when
|
|
|
|
|
* // this state change jump is true
|
|
|
|
|
* transition('on => off', [
|
|
|
|
|
* animate("1s")
|
|
|
|
|
* ])
|
|
|
|
|
* ])
|
|
|
|
|
* ]
|
|
|
|
|
* })
|
|
|
|
|
* ```
|
|
|
|
|
*
|
|
|
|
|
* As depicted in the code above, a group of related animation states are all contained within
|
|
|
|
|
* an animation `trigger` (the code example above called the trigger `myTriggerName`).
|
|
|
|
|
* When a trigger is created then it can be bound onto an element within the component's
|
2016-06-08 16:38:52 -07:00
|
|
|
* template via a property prefixed by an `@` symbol followed by trigger name and an expression
|
|
|
|
|
* that
|
2016-05-26 09:22:44 -07:00
|
|
|
* is used to determine the state value for that trigger.
|
|
|
|
|
*
|
|
|
|
|
* ```html
|
|
|
|
|
* <!-- animation-cmp.html -->
|
|
|
|
|
* <div @myTriggerName="expression">...</div>
|
|
|
|
|
* ```
|
|
|
|
|
*
|
2016-06-08 16:38:52 -07:00
|
|
|
* For state changes to be executed, the `expression` value must change value from its existing
|
|
|
|
|
* value
|
2016-05-26 09:22:44 -07:00
|
|
|
* to something that we have set an animation to animate on (in the example above we are listening
|
|
|
|
|
* to a change of state between `on` and `off`). The `expression` value attached to the trigger
|
|
|
|
|
* must be something that can be evaluated with the template/component context.
|
|
|
|
|
*
|
|
|
|
|
* ### DSL Animation Functions
|
|
|
|
|
*
|
|
|
|
|
* Please visit each of the animation DSL functions listed below to gain a better understanding
|
|
|
|
|
* of how and why they are used for crafting animations in Angular2:
|
|
|
|
|
*
|
|
|
|
|
* - {@link trigger trigger()}
|
|
|
|
|
* - {@link state state()}
|
|
|
|
|
* - {@link transition transition()}
|
|
|
|
|
* - {@link group group()}
|
|
|
|
|
* - {@link sequence sequence()}
|
|
|
|
|
* - {@link style style()}
|
|
|
|
|
* - {@link animate animate()}
|
|
|
|
|
* - {@link keyframes keyframes()}
|
|
|
|
|
*/
|
2016-05-25 12:46:22 -07:00
|
|
|
animations: AnimationEntryMetadata[];
|
|
|
|
|
|
2016-06-08 16:38:52 -07:00
|
|
|
directives: Array<Type|any[]>;
|
2015-10-06 17:03:37 -07:00
|
|
|
|
2016-06-08 16:38:52 -07:00
|
|
|
pipes: Array<Type|any[]>;
|
2015-10-06 17:03:37 -07:00
|
|
|
|
2016-07-28 06:31:26 -07:00
|
|
|
/**
|
|
|
|
|
* Specify how the template and the styles should be encapsulated.
|
|
|
|
|
* The default is {@link ViewEncapsulation#Emulated `ViewEncapsulation.Emulated`} if the view
|
|
|
|
|
* has styles,
|
|
|
|
|
* otherwise {@link ViewEncapsulation#None `ViewEncapsulation.None`}.
|
|
|
|
|
*/
|
2015-10-06 17:03:37 -07:00
|
|
|
encapsulation: ViewEncapsulation;
|
|
|
|
|
|
2016-06-20 09:52:41 -07:00
|
|
|
interpolation: [string, string];
|
|
|
|
|
|
2016-06-22 14:06:23 -07:00
|
|
|
/**
|
2016-07-25 00:36:30 -07:00
|
|
|
* Defines the components that should be compiled as well when
|
2016-06-22 14:06:23 -07:00
|
|
|
* this component is defined. For each components listed here,
|
|
|
|
|
* Angular will create a {@link ComponentFactory ComponentFactory} and store it in the
|
|
|
|
|
* {@link ComponentFactoryResolver ComponentFactoryResolver}.
|
|
|
|
|
*/
|
2016-07-25 00:36:30 -07:00
|
|
|
entryComponents: Array<Type|any[]>;
|
2016-06-22 14:06:23 -07:00
|
|
|
|
2016-07-29 02:10:30 -07:00
|
|
|
constructor({selector,
|
|
|
|
|
inputs,
|
|
|
|
|
outputs,
|
|
|
|
|
properties,
|
|
|
|
|
events,
|
|
|
|
|
host,
|
|
|
|
|
exportAs,
|
|
|
|
|
moduleId,
|
|
|
|
|
providers,
|
|
|
|
|
viewProviders,
|
|
|
|
|
changeDetection = ChangeDetectionStrategy.Default,
|
|
|
|
|
queries,
|
|
|
|
|
templateUrl,
|
|
|
|
|
template,
|
|
|
|
|
styleUrls,
|
|
|
|
|
styles,
|
|
|
|
|
animations,
|
|
|
|
|
directives,
|
|
|
|
|
pipes,
|
|
|
|
|
encapsulation,
|
|
|
|
|
interpolation,
|
|
|
|
|
entryComponents}: ComponentMetadataType = {}) {
|
2015-07-07 22:09:19 +02:00
|
|
|
super({
|
|
|
|
|
selector: selector,
|
2015-09-30 20:59:23 -07:00
|
|
|
inputs: inputs,
|
|
|
|
|
outputs: outputs,
|
2015-10-02 16:21:49 -07:00
|
|
|
properties: properties,
|
|
|
|
|
events: events,
|
2015-07-07 22:09:19 +02:00
|
|
|
host: host,
|
|
|
|
|
exportAs: exportAs,
|
2015-10-10 22:11:13 -07:00
|
|
|
providers: providers,
|
2015-10-01 10:07:49 -07:00
|
|
|
queries: queries
|
2015-07-07 22:09:19 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.changeDetection = changeDetection;
|
2015-10-10 22:11:13 -07:00
|
|
|
this._viewProviders = viewProviders;
|
2015-10-06 17:03:37 -07:00
|
|
|
this.templateUrl = templateUrl;
|
|
|
|
|
this.template = template;
|
|
|
|
|
this.styleUrls = styleUrls;
|
|
|
|
|
this.styles = styles;
|
|
|
|
|
this.directives = directives;
|
|
|
|
|
this.pipes = pipes;
|
|
|
|
|
this.encapsulation = encapsulation;
|
2015-12-13 17:35:33 -08:00
|
|
|
this.moduleId = moduleId;
|
2016-05-25 12:46:22 -07:00
|
|
|
this.animations = animations;
|
2016-06-20 09:52:41 -07:00
|
|
|
this.interpolation = interpolation;
|
2016-07-29 02:10:30 -07:00
|
|
|
this.entryComponents = entryComponents;
|
2015-07-07 22:09:19 +02:00
|
|
|
}
|
2015-06-12 23:51:42 -07:00
|
|
|
}
|
|
|
|
|
|
2016-07-29 02:10:30 -07:00
|
|
|
/**
|
|
|
|
|
* Interface for creating {@link PipeMetadata}
|
|
|
|
|
* @experimental
|
|
|
|
|
*/
|
|
|
|
|
export interface PipeMetadataType {
|
|
|
|
|
name: string;
|
|
|
|
|
pure?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-07 11:41:38 -07:00
|
|
|
/**
|
|
|
|
|
* Declare reusable pipe function.
|
|
|
|
|
*
|
2015-10-27 16:37:08 -07:00
|
|
|
* A "pure" pipe is only re-evaluated when either the input or any of the arguments change.
|
2015-10-27 12:42:35 -07:00
|
|
|
*
|
|
|
|
|
* When not specified, pipes default to being pure.
|
|
|
|
|
*
|
2015-10-19 15:37:32 +01:00
|
|
|
* ### Example
|
2015-08-07 11:41:38 -07:00
|
|
|
*
|
2015-11-30 08:28:54 -08:00
|
|
|
* {@example core/ts/metadata/metadata.ts region='pipe'}
|
2016-05-25 15:00:05 -07:00
|
|
|
* @stable
|
2015-08-07 11:41:38 -07:00
|
|
|
*/
|
2016-07-29 02:10:30 -07:00
|
|
|
export class PipeMetadata extends InjectableMetadata implements PipeMetadataType {
|
2015-08-07 11:41:38 -07:00
|
|
|
name: string;
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2015-09-08 09:17:58 -07:00
|
|
|
_pure: boolean;
|
2015-08-07 11:41:38 -07:00
|
|
|
|
2016-07-29 02:10:30 -07:00
|
|
|
constructor({name, pure}: PipeMetadataType) {
|
2015-08-07 11:41:38 -07:00
|
|
|
super();
|
|
|
|
|
this.name = name;
|
2015-09-08 09:17:58 -07:00
|
|
|
this._pure = pure;
|
2015-08-07 11:41:38 -07:00
|
|
|
}
|
2015-09-08 09:17:58 -07:00
|
|
|
|
|
|
|
|
get pure(): boolean { return isPresent(this._pure) ? this._pure : true; }
|
2015-08-07 11:41:38 -07:00
|
|
|
}
|
2015-09-03 15:10:48 -07:00
|
|
|
|
|
|
|
|
/**
|
2015-09-30 20:59:23 -07:00
|
|
|
* Declares a data-bound input property.
|
2015-09-03 15:10:48 -07:00
|
|
|
*
|
2015-09-24 13:46:13 -07:00
|
|
|
* Angular automatically updates data-bound properties during change detection.
|
2015-09-03 15:10:48 -07:00
|
|
|
*
|
2015-10-01 15:47:51 -07:00
|
|
|
* `InputMetadata` takes an optional parameter that specifies the name
|
2015-09-24 13:46:13 -07:00
|
|
|
* used when instantiating a component in the template. When not provided,
|
2015-10-01 15:47:51 -07:00
|
|
|
* the name of the decorated property is used.
|
2015-09-24 13:46:13 -07:00
|
|
|
*
|
|
|
|
|
* ### Example
|
|
|
|
|
*
|
2015-09-30 20:59:23 -07:00
|
|
|
* The following example creates a component with two input properties.
|
2015-09-24 13:46:13 -07:00
|
|
|
*
|
|
|
|
|
* ```typescript
|
2015-10-11 07:41:19 -07:00
|
|
|
* @Component({
|
|
|
|
|
* selector: 'bank-account',
|
2015-09-24 13:46:13 -07:00
|
|
|
* template: `
|
|
|
|
|
* Bank Name: {{bankName}}
|
|
|
|
|
* Account Id: {{id}}
|
|
|
|
|
* `
|
2015-09-03 15:10:48 -07:00
|
|
|
* })
|
2015-09-24 13:46:13 -07:00
|
|
|
* class BankAccount {
|
2015-09-30 20:59:23 -07:00
|
|
|
* @Input() bankName: string;
|
|
|
|
|
* @Input('account-id') id: string;
|
2015-09-24 13:46:13 -07:00
|
|
|
*
|
|
|
|
|
* // this property is not bound, and won't be automatically updated by Angular
|
|
|
|
|
* normalizedBankName: string;
|
2015-09-03 15:10:48 -07:00
|
|
|
* }
|
2015-09-24 13:46:13 -07:00
|
|
|
*
|
2015-10-11 07:41:19 -07:00
|
|
|
* @Component({
|
|
|
|
|
* selector: 'app',
|
2015-09-24 13:46:13 -07:00
|
|
|
* template: `
|
|
|
|
|
* <bank-account bank-name="RBC" account-id="4747"></bank-account>
|
|
|
|
|
* `,
|
|
|
|
|
* directives: [BankAccount]
|
|
|
|
|
* })
|
|
|
|
|
* class App {}
|
|
|
|
|
*
|
|
|
|
|
* bootstrap(App);
|
2015-09-03 15:10:48 -07:00
|
|
|
* ```
|
2016-05-25 15:00:05 -07:00
|
|
|
* @stable
|
2015-09-03 15:10:48 -07:00
|
|
|
*/
|
2015-09-30 20:59:23 -07:00
|
|
|
export class InputMetadata {
|
2015-09-24 13:46:13 -07:00
|
|
|
constructor(
|
|
|
|
|
/**
|
2016-03-10 11:28:06 -08:00
|
|
|
* Name used when instantiating a component in the template.
|
2015-09-24 13:46:13 -07:00
|
|
|
*/
|
|
|
|
|
public bindingPropertyName?: string) {}
|
2015-09-03 15:10:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-09-30 20:59:23 -07:00
|
|
|
* Declares an event-bound output property.
|
2015-09-03 15:10:48 -07:00
|
|
|
*
|
2015-09-30 20:59:23 -07:00
|
|
|
* When an output property emits an event, an event handler attached to that event
|
2015-09-24 13:46:13 -07:00
|
|
|
* the template is invoked.
|
2015-09-03 15:10:48 -07:00
|
|
|
*
|
2015-10-01 15:47:51 -07:00
|
|
|
* `OutputMetadata` takes an optional parameter that specifies the name
|
2015-09-24 13:46:13 -07:00
|
|
|
* used when instantiating a component in the template. When not provided,
|
2015-10-01 15:47:51 -07:00
|
|
|
* the name of the decorated property is used.
|
2015-09-24 13:46:13 -07:00
|
|
|
*
|
|
|
|
|
* ### Example
|
|
|
|
|
*
|
|
|
|
|
* ```typescript
|
2015-09-03 15:10:48 -07:00
|
|
|
* @Directive({
|
2015-09-24 13:46:13 -07:00
|
|
|
* selector: 'interval-dir',
|
2015-09-03 15:10:48 -07:00
|
|
|
* })
|
2015-09-24 13:46:13 -07:00
|
|
|
* class IntervalDir {
|
2015-09-30 20:59:23 -07:00
|
|
|
* @Output() everySecond = new EventEmitter();
|
|
|
|
|
* @Output('everyFiveSeconds') five5Secs = new EventEmitter();
|
2015-09-24 13:46:13 -07:00
|
|
|
*
|
|
|
|
|
* constructor() {
|
2015-11-15 23:58:59 -08:00
|
|
|
* setInterval(() => this.everySecond.emit("event"), 1000);
|
|
|
|
|
* setInterval(() => this.five5Secs.emit("event"), 5000);
|
2015-09-24 13:46:13 -07:00
|
|
|
* }
|
2015-09-03 15:10:48 -07:00
|
|
|
* }
|
2015-09-24 13:46:13 -07:00
|
|
|
*
|
2015-10-11 07:41:19 -07:00
|
|
|
* @Component({
|
|
|
|
|
* selector: 'app',
|
2015-09-24 13:46:13 -07:00
|
|
|
* template: `
|
2016-01-22 23:10:48 -08:00
|
|
|
* <interval-dir (everySecond)="everySecond()" (everyFiveSeconds)="everyFiveSeconds()">
|
2015-09-24 13:46:13 -07:00
|
|
|
* </interval-dir>
|
|
|
|
|
* `,
|
|
|
|
|
* directives: [IntervalDir]
|
|
|
|
|
* })
|
|
|
|
|
* class App {
|
|
|
|
|
* everySecond() { console.log('second'); }
|
|
|
|
|
* everyFiveSeconds() { console.log('five seconds'); }
|
|
|
|
|
* }
|
|
|
|
|
* bootstrap(App);
|
2015-09-03 15:10:48 -07:00
|
|
|
* ```
|
2016-05-25 15:00:05 -07:00
|
|
|
* @stable
|
2015-09-03 15:10:48 -07:00
|
|
|
*/
|
2015-09-30 20:59:23 -07:00
|
|
|
export class OutputMetadata {
|
2015-09-03 15:10:48 -07:00
|
|
|
constructor(public bindingPropertyName?: string) {}
|
2015-09-04 14:07:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-10-01 15:47:51 -07:00
|
|
|
* Declares a host property binding.
|
2015-09-04 14:07:16 -07:00
|
|
|
*
|
2015-10-01 15:47:51 -07:00
|
|
|
* Angular automatically checks host property bindings during change detection.
|
|
|
|
|
* If a binding changes, it will update the host element of the directive.
|
|
|
|
|
*
|
|
|
|
|
* `HostBindingMetadata` takes an optional parameter that specifies the property
|
|
|
|
|
* name of the host element that will be updated. When not provided,
|
|
|
|
|
* the class property name is used.
|
2015-09-24 13:46:13 -07:00
|
|
|
*
|
|
|
|
|
* ### Example
|
2015-09-04 14:07:16 -07:00
|
|
|
*
|
2015-10-01 15:47:51 -07:00
|
|
|
* The following example creates a directive that sets the `valid` and `invalid` classes
|
2015-11-23 16:02:19 -08:00
|
|
|
* on the DOM element that has ngModel directive on it.
|
2015-09-04 14:07:16 -07:00
|
|
|
*
|
2015-10-01 15:47:51 -07:00
|
|
|
* ```typescript
|
2015-11-23 16:02:19 -08:00
|
|
|
* @Directive({selector: '[ngModel]'})
|
2015-10-01 15:47:51 -07:00
|
|
|
* class NgModelStatus {
|
|
|
|
|
* constructor(public control:NgModel) {}
|
2016-02-07 12:51:20 -06:00
|
|
|
* @HostBinding('class.valid') get valid { return this.control.valid; }
|
|
|
|
|
* @HostBinding('class.invalid') get invalid { return this.control.invalid; }
|
2015-10-01 15:47:51 -07:00
|
|
|
* }
|
2015-09-04 14:07:16 -07:00
|
|
|
*
|
2015-10-11 07:41:19 -07:00
|
|
|
* @Component({
|
|
|
|
|
* selector: 'app',
|
2015-11-23 16:02:19 -08:00
|
|
|
* template: `<input [(ngModel)]="prop">`,
|
2015-10-01 15:47:51 -07:00
|
|
|
* directives: [FORM_DIRECTIVES, NgModelStatus]
|
2015-09-04 14:07:16 -07:00
|
|
|
* })
|
2015-10-01 15:47:51 -07:00
|
|
|
* class App {
|
|
|
|
|
* prop;
|
2015-09-04 14:07:16 -07:00
|
|
|
* }
|
2015-10-01 15:47:51 -07:00
|
|
|
*
|
|
|
|
|
* bootstrap(App);
|
2015-09-04 14:07:16 -07:00
|
|
|
* ```
|
2016-05-25 15:00:05 -07:00
|
|
|
* @stable
|
2015-09-04 14:07:16 -07:00
|
|
|
*/
|
|
|
|
|
export class HostBindingMetadata {
|
|
|
|
|
constructor(public hostPropertyName?: string) {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-10-01 15:47:51 -07:00
|
|
|
* Declares a host listener.
|
2015-09-04 14:07:16 -07:00
|
|
|
*
|
2015-10-01 15:47:51 -07:00
|
|
|
* Angular will invoke the decorated method when the host element emits the specified event.
|
2015-09-04 14:07:16 -07:00
|
|
|
*
|
2015-10-01 15:47:51 -07:00
|
|
|
* If the decorated method returns `false`, then `preventDefault` is applied on the DOM
|
|
|
|
|
* event.
|
2015-09-04 14:07:16 -07:00
|
|
|
*
|
2015-10-01 15:47:51 -07:00
|
|
|
* ### Example
|
2015-09-04 14:07:16 -07:00
|
|
|
*
|
2015-10-01 15:47:51 -07:00
|
|
|
* The following example declares a directive that attaches a click listener to the button and
|
|
|
|
|
* counts clicks.
|
|
|
|
|
*
|
|
|
|
|
* ```typescript
|
|
|
|
|
* @Directive({selector: 'button[counting]'})
|
|
|
|
|
* class CountClicks {
|
|
|
|
|
* numberOfClicks = 0;
|
|
|
|
|
*
|
|
|
|
|
* @HostListener('click', ['$event.target'])
|
|
|
|
|
* onClick(btn) {
|
|
|
|
|
* console.log("button", btn, "number of clicks:", this.numberOfClicks++);
|
|
|
|
|
* }
|
2015-09-04 14:07:16 -07:00
|
|
|
* }
|
2015-10-01 15:47:51 -07:00
|
|
|
*
|
2015-10-11 07:41:19 -07:00
|
|
|
* @Component({
|
|
|
|
|
* selector: 'app',
|
2015-10-01 15:47:51 -07:00
|
|
|
* template: `<button counting>Increment</button>`,
|
|
|
|
|
* directives: [CountClicks]
|
|
|
|
|
* })
|
|
|
|
|
* class App {}
|
|
|
|
|
*
|
|
|
|
|
* bootstrap(App);
|
2015-09-04 14:07:16 -07:00
|
|
|
* ```
|
2016-05-25 15:00:05 -07:00
|
|
|
* @stable
|
2015-09-04 14:07:16 -07:00
|
|
|
*/
|
|
|
|
|
export class HostListenerMetadata {
|
|
|
|
|
constructor(public eventName: string, public args?: string[]) {}
|
2015-09-13 14:30:55 +02:00
|
|
|
}
|