2015-08-20 14:28:25 -07:00
|
|
|
import {CONST, CONST_EXPR} from 'angular2/src/core/facade/lang';
|
2015-09-03 22:01:36 -07:00
|
|
|
import {ChangeDetectionStrategy} from 'angular2/src/core/change_detection';
|
2015-08-20 14:28:25 -07:00
|
|
|
import {InjectableMetadata} from 'angular2/src/core/di/metadata';
|
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
|
2015-08-14 10:03:45 -07:00
|
|
|
* {@link ViewMetadata}:
|
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-03-19 16:56:45 +00: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-05-26 15:54:10 +02:00
|
|
|
* properties: [
|
|
|
|
* '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,
|
|
|
|
* removed, or moved by a directive that uses a {@link ViewContainerRef} such as a `ng-for`, an
|
|
|
|
* `ng-if`, or an `ng-switch`.
|
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-06-02 08:55:58 -07:00
|
|
|
* 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-04-30 13:38:40 -07:00
|
|
|
* ## Example
|
|
|
|
*
|
|
|
|
* Here we use a decorator directive to simply define basic tool-tip behavior.
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @Directive({
|
|
|
|
* selector: '[tooltip]',
|
2015-05-26 15:54:10 +02:00
|
|
|
* properties: [
|
|
|
|
* '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.
|
|
|
|
*
|
2015-08-14 10:03:45 -07:00
|
|
|
* Views are always created as children of the current {@link ViewMetadata}, 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
|
|
|
|
*
|
|
|
|
* When the directive class implements some {@link angular2/lifecycle_hooks} the 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
|
|
|
*
|
|
|
|
* ## Example
|
|
|
|
*
|
|
|
|
* 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-05-26 15:54:10 +02:00
|
|
|
* properties: ['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.
|
2015-03-13 21:54:19 +00:00
|
|
|
*/
|
2015-05-20 09:48:15 -07:00
|
|
|
@CONST()
|
2015-08-14 10:03:45 -07:00
|
|
|
export class DirectiveMetadata extends InjectableMetadata {
|
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-03-14 00:00:42 +00: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
|
|
|
/**
|
|
|
|
* Enumerates the set of properties that accept data binding for a directive.
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-04-09 21:20:11 +02:00
|
|
|
* The `properties` 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-08-14 10:03:45 -07:00
|
|
|
* You can include a {@link PipeMetadata} when specifying a `bindingProperty` to allow for data
|
2015-05-26 15:54:10 +02:00
|
|
|
* transformation and structural change detection of the value. These pipes will be evaluated in
|
|
|
|
* the context of this component.
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-03-14 00:00:42 +00:00
|
|
|
* ## Syntax
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-05-26 15:54:10 +02:00
|
|
|
* There is no need to specify both `directiveProperty` and `bindingProperty` when they both have
|
|
|
|
* the same value.
|
|
|
|
*
|
2015-03-17 19:22:13 +00:00
|
|
|
* ```
|
|
|
|
* @Directive({
|
2015-05-26 15:54:10 +02:00
|
|
|
* properties: [
|
|
|
|
* 'propertyName', // shorthand notation for 'propertyName: propertyName'
|
|
|
|
* 'directiveProperty1: bindingProperty1',
|
|
|
|
* 'directiveProperty2: bindingProperty2 | pipe1 | ...',
|
2015-03-17 19:22:13 +00:00
|
|
|
* ...
|
2015-05-26 15:54:10 +02:00
|
|
|
* ]
|
2015-03-17 19:22:13 +00:00
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
*
|
2015-03-20 21:11:58 +00:00
|
|
|
* ## Basic Property Binding
|
|
|
|
*
|
2015-05-20 09:48:15 -07:00
|
|
|
* We can easily build a simple `Tooltip` directive that exposes a `tooltip` property, which can
|
2015-05-26 15:54:10 +02:00
|
|
|
* be used in templates with standard Angular syntax. For example:
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
|
|
|
* ```
|
2015-04-30 13:38:40 -07:00
|
|
|
* @Directive({
|
2015-03-17 19:22:13 +00:00
|
|
|
* selector: '[tooltip]',
|
2015-05-26 15:54:10 +02:00
|
|
|
* properties: [
|
|
|
|
* 'text: tooltip'
|
|
|
|
* ]
|
2015-03-17 19:22:13 +00:00
|
|
|
* })
|
|
|
|
* class Tooltip {
|
2015-05-26 15:54:10 +02:00
|
|
|
* set text(value: string) {
|
|
|
|
* // This will get called every time with the new value when the 'tooltip' property changes
|
2015-03-17 19:22:13 +00:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
2015-05-20 09:48:15 -07:00
|
|
|
* We can then bind to the `tooltip' property as either an expression (`someExpression`) or as a
|
2015-05-26 15:54:10 +02:00
|
|
|
* string literal, as shown in the HTML template below:
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
|
|
|
* ```html
|
2015-03-20 21:11:58 +00:00
|
|
|
* <div [tooltip]="someExpression">...</div>
|
|
|
|
* <div tooltip="Some Text">...</div>
|
2015-03-17 19:22:13 +00:00
|
|
|
* ```
|
|
|
|
*
|
2015-04-09 21:20:11 +02:00
|
|
|
* Whenever the `someExpression` expression changes, the `properties` declaration instructs
|
|
|
|
* Angular to update the `Tooltip`'s `text` property.
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-08-11 10:40:51 -07:00
|
|
|
* ### Bindings With Pipes
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-08-11 10:40:51 -07:00
|
|
|
* You can use pipes in bindings, as follows:
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
|
|
|
* ```html
|
2015-03-20 21:11:58 +00:00
|
|
|
* <div [class-set]="someExpression | somePipe">
|
2015-03-17 19:22:13 +00:00
|
|
|
* ```
|
|
|
|
*
|
2015-03-14 00:00:42 +00:00
|
|
|
*/
|
2015-08-28 11:29:19 -07:00
|
|
|
properties: string[];
|
2015-03-17 19:22:13 +00:00
|
|
|
|
2015-04-14 14:34:41 -07:00
|
|
|
/**
|
|
|
|
* Enumerates the set of emitted events.
|
|
|
|
*
|
|
|
|
* ## Syntax
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @Component({
|
2015-04-21 23:21:24 +02:00
|
|
|
* events: ['statusChange']
|
2015-04-14 14:34:41 -07:00
|
|
|
* })
|
|
|
|
* class TaskComponent {
|
2015-06-04 15:09:54 +02:00
|
|
|
* statusChange: EventEmitter;
|
2015-04-14 14:34:41 -07:00
|
|
|
*
|
|
|
|
* constructor() {
|
2015-04-21 23:21:24 +02:00
|
|
|
* this.statusChange = new EventEmitter();
|
2015-04-14 14:34:41 -07:00
|
|
|
* }
|
|
|
|
*
|
|
|
|
* onComplete() {
|
2015-04-21 23:21:24 +02:00
|
|
|
* this.statusChange.next('completed');
|
2015-04-14 14:34:41 -07:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
2015-06-04 15:09:54 +02:00
|
|
|
*
|
|
|
|
* Use `propertyName: eventName` when the event emitter property name is different from the name
|
|
|
|
* of the emitted event:
|
|
|
|
*
|
2015-06-09 23:11:08 +01:00
|
|
|
* ```
|
2015-06-04 15:09:54 +02:00
|
|
|
* @Component({
|
|
|
|
* events: ['status: statusChange']
|
|
|
|
* })
|
|
|
|
* class TaskComponent {
|
|
|
|
* status: EventEmitter;
|
|
|
|
*
|
|
|
|
* constructor() {
|
|
|
|
* this.status = new EventEmitter();
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* onComplete() {
|
|
|
|
* this.status.next('completed');
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
2015-04-14 14:34:41 -07:00
|
|
|
*/
|
2015-08-28 11:29:19 -07:00
|
|
|
events: string[];
|
2015-04-14 14:34:41 -07:00
|
|
|
|
2015-03-14 00:00:42 +00:00
|
|
|
/**
|
2015-06-09 12:46:21 +02:00
|
|
|
* Specifiy the events, actions, properties and attributes related to the host element.
|
|
|
|
*
|
|
|
|
* ## Events
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-06-09 12:46:21 +02:00
|
|
|
* Specifies which DOM hostListeners a directive listens to via a set of `(event)` to `method`
|
|
|
|
* key-value pairs:
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-03-20 21:11:58 +00:00
|
|
|
* - `event1`: 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-05-20 09:48:15 -07:00
|
|
|
* If the evalutation of the statement returns `false`, then `preventDefault`is applied on the DOM
|
|
|
|
* 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
|
|
|
*
|
|
|
|
* When writing a directive event binding, you can also refer to the following local variables:
|
2015-04-03 22:07:15 +02:00
|
|
|
* - `$event`: Current event object which triggered the event.
|
2015-05-20 09:48:15 -07:00
|
|
|
* - `$target`: The source of the event. This will be either a DOM element or an Angular
|
2015-06-09 12:46:21 +02:00
|
|
|
* directive. (will be implemented in later release)
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-03-14 00:00:42 +00:00
|
|
|
* ## Syntax
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @Directive({
|
2015-06-09 12:46:21 +02:00
|
|
|
* host: {
|
|
|
|
* '(event1)': 'onMethod1(arguments)',
|
|
|
|
* '(target:event2)': 'onMethod2(arguments)',
|
2015-03-17 19:22:13 +00:00
|
|
|
* ...
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* ## Basic Event Binding:
|
|
|
|
*
|
2015-06-09 12:46:21 +02:00
|
|
|
* Suppose you want to write a directive that reacts to `change` events in the DOM and on
|
2015-05-20 09:48:15 -07:00
|
|
|
* `resize` events in window.
|
2015-04-02 15:56:58 +02:00
|
|
|
* You would define the event binding as follows:
|
2015-03-20 21:11:58 +00:00
|
|
|
*
|
2015-03-17 19:22:13 +00:00
|
|
|
* ```
|
2015-04-30 13:38:40 -07:00
|
|
|
* @Directive({
|
2015-03-17 19:22:13 +00:00
|
|
|
* selector: 'input',
|
2015-06-09 12:46:21 +02:00
|
|
|
* host: {
|
|
|
|
* '(change)': 'onChange($event)',
|
|
|
|
* '(window:resize)': 'onResize($event)'
|
2015-03-17 19:22:13 +00:00
|
|
|
* }
|
|
|
|
* })
|
2015-04-30 13:38:40 -07:00
|
|
|
* class InputDirective {
|
2015-03-17 19:22:13 +00:00
|
|
|
* onChange(event:Event) {
|
2015-06-09 12:46:21 +02:00
|
|
|
* // invoked when the input element fires the 'change' event
|
2015-03-17 19:22:13 +00:00
|
|
|
* }
|
2015-04-02 15:56:58 +02:00
|
|
|
* onResize(event:Event) {
|
2015-06-09 12:46:21 +02:00
|
|
|
* // invoked when the window fires the 'resize' event
|
2015-04-02 15:56:58 +02:00
|
|
|
* }
|
2015-03-17 19:22:13 +00:00
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
2015-06-09 12:46:21 +02:00
|
|
|
* ## Properties
|
2015-03-20 21:11:58 +00:00
|
|
|
*
|
2015-04-21 11:47:53 -07:00
|
|
|
* Specifies which DOM properties a directives updates.
|
|
|
|
*
|
|
|
|
* ## Syntax
|
|
|
|
*
|
|
|
|
* ```
|
2015-04-30 13:38:40 -07:00
|
|
|
* @Directive({
|
2015-04-21 11:47:53 -07:00
|
|
|
* selector: 'input',
|
2015-06-09 12:46:21 +02:00
|
|
|
* host: {
|
|
|
|
* '[prop]': 'expression'
|
2015-04-21 11:47:53 -07:00
|
|
|
* }
|
|
|
|
* })
|
2015-04-30 13:38:40 -07:00
|
|
|
* class InputDirective {
|
2015-04-21 11:47:53 -07:00
|
|
|
* value:string;
|
|
|
|
* }
|
|
|
|
* ```
|
2015-06-09 12:46:21 +02:00
|
|
|
*
|
|
|
|
* In this example the prop property of the host element is updated with the expression value
|
|
|
|
* every time it changes.
|
|
|
|
*
|
|
|
|
* ## Attributes
|
|
|
|
*
|
2015-05-20 09:48:15 -07:00
|
|
|
* Specifies static attributes that should be propagated to a host element. Attributes specified
|
2015-06-09 12:46:21 +02:00
|
|
|
* in `hostAttributes` are propagated only if a given attribute is not present on a host element.
|
2015-05-01 13:41:56 +02:00
|
|
|
*
|
|
|
|
* ## Syntax
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @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-01 13:41:56 +02:00
|
|
|
*
|
2015-05-20 09:48:15 -07:00
|
|
|
* In this example using `my-button` directive (ex.: `<div my-button></div>`) on a host element
|
2015-06-09 12:46:21 +02:00
|
|
|
* (here: `<div>` ) will ensure that this element will get the "button" role.
|
|
|
|
*
|
2015-05-11 12:31:16 -07:00
|
|
|
*/
|
2015-07-07 22:09:19 +02:00
|
|
|
host: StringMap<string, string>;
|
2015-05-11 12:31:16 -07:00
|
|
|
|
2015-07-07 22:09:19 +02:00
|
|
|
/**
|
|
|
|
* If set to false the compiler does not compile the children of this directive.
|
|
|
|
*/
|
|
|
|
// TODO(vsavkin): This would better fall under the Macro directive concept.
|
|
|
|
compileChildren: boolean;
|
2015-04-30 13:38:40 -07:00
|
|
|
|
2015-05-16 11:01:02 -07:00
|
|
|
/**
|
2015-05-20 09:48:15 -07:00
|
|
|
* Defines the set of injectable objects that are visible to a Directive and its light 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: 'greet',
|
2015-07-29 15:01:22 -07:00
|
|
|
* bindings: [
|
2015-05-16 11:01:02 -07:00
|
|
|
* Greeter
|
|
|
|
* ]
|
|
|
|
* })
|
|
|
|
* class HelloWorld {
|
|
|
|
* greeter:Greeter;
|
|
|
|
*
|
|
|
|
* constructor(greeter:Greeter) {
|
|
|
|
* this.greeter = greeter;
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*/
|
2015-08-28 11:29:19 -07:00
|
|
|
bindings: 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',
|
|
|
|
* })
|
|
|
|
* @View({
|
|
|
|
* 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-07-07 22:09:19 +02:00
|
|
|
constructor({
|
2015-08-31 18:32:32 -07:00
|
|
|
selector, properties, events, host, bindings, exportAs, compileChildren = true,
|
2015-07-07 22:09:19 +02:00
|
|
|
}: {
|
|
|
|
selector?: string,
|
2015-08-28 11:29:19 -07:00
|
|
|
properties?: string[],
|
|
|
|
events?: string[],
|
2015-07-07 22:09:19 +02:00
|
|
|
host?: StringMap<string, string>,
|
2015-08-28 11:29:19 -07:00
|
|
|
bindings?: any[],
|
2015-07-07 22:09:19 +02:00
|
|
|
exportAs?: string,
|
|
|
|
compileChildren?: boolean,
|
|
|
|
} = {}) {
|
2015-07-13 15:48:28 -07:00
|
|
|
super();
|
2015-07-07 22:09:19 +02:00
|
|
|
this.selector = selector;
|
|
|
|
this.properties = properties;
|
|
|
|
this.events = events;
|
|
|
|
this.host = host;
|
|
|
|
this.exportAs = exportAs;
|
|
|
|
this.compileChildren = compileChildren;
|
2015-07-29 15:01:22 -07:00
|
|
|
this.bindings = bindings;
|
2015-07-07 22:09:19 +02:00
|
|
|
}
|
2015-06-12 23:51:42 -07:00
|
|
|
}
|
|
|
|
|
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-05-20 09:48:15 -07:00
|
|
|
* Each Angular component requires a single `@Component` and at least one `@View` annotation. The
|
|
|
|
* `@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-07-29 15:01:22 -07:00
|
|
|
* - creates all the injectable objects configured with `bindings` and `viewBindings`.
|
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-08-14 10:03:45 -07:00
|
|
|
* For details on the `@View` annotation, see {@link ViewMetadata}.
|
2015-03-17 23:29:27 +00:00
|
|
|
*
|
2015-09-02 16:43:39 -07:00
|
|
|
* ## Lifecycle hooks
|
|
|
|
*
|
|
|
|
* When the component class implements some {@link angular2/lifecycle_hooks} the callbacks are
|
|
|
|
* called by the change detection at defined points in time during the life of the component.
|
|
|
|
*
|
2015-03-17 23:29:27 +00:00
|
|
|
* ## Example
|
2015-03-20 21:11:58 +00:00
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @Component({
|
|
|
|
* selector: 'greet'
|
|
|
|
* })
|
2015-04-09 21:20:11 +02:00
|
|
|
* @View({
|
|
|
|
* template: 'Hello {{name}}!'
|
2015-03-20 21:11:58 +00:00
|
|
|
* })
|
|
|
|
* class Greet {
|
|
|
|
* name: string;
|
|
|
|
*
|
|
|
|
* constructor() {
|
|
|
|
* this.name = 'World';
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
2015-03-17 23:29:27 +00:00
|
|
|
*
|
2015-03-17 19:22:13 +00:00
|
|
|
*/
|
2015-05-20 09:48:15 -07:00
|
|
|
@CONST()
|
2015-08-14 10:03:45 -07:00
|
|
|
export class ComponentMetadata extends DirectiveMetadata {
|
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
|
|
|
/**
|
|
|
|
* Defines the set of injectable objects that are visible to its view dom children.
|
|
|
|
*
|
|
|
|
* ## 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-07-29 15:01:22 -07:00
|
|
|
* viewBindings: [
|
2015-05-16 11:01:02 -07:00
|
|
|
* Greeter
|
|
|
|
* ]
|
|
|
|
* })
|
|
|
|
* @View({
|
|
|
|
* template: `<needs-greeter></needs-greeter>`,
|
|
|
|
* directives: [NeedsGreeter]
|
|
|
|
* })
|
|
|
|
* class HelloWorld {
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
*/
|
2015-08-28 11:29:19 -07:00
|
|
|
viewBindings: any[];
|
2015-07-06 16:32:29 -07:00
|
|
|
|
2015-08-31 18:32:32 -07:00
|
|
|
constructor({selector, properties, events, host, exportAs, bindings, viewBindings,
|
2015-08-26 11:44:59 -07:00
|
|
|
changeDetection = ChangeDetectionStrategy.Default, compileChildren = true}: {
|
2015-07-07 22:09:19 +02:00
|
|
|
selector?: string,
|
2015-08-28 11:29:19 -07:00
|
|
|
properties?: string[],
|
|
|
|
events?: string[],
|
2015-07-07 22:09:19 +02:00
|
|
|
host?: StringMap<string, string>,
|
2015-08-28 11:29:19 -07:00
|
|
|
bindings?: any[],
|
2015-07-07 22:09:19 +02:00
|
|
|
exportAs?: string,
|
|
|
|
compileChildren?: boolean,
|
2015-08-28 11:29:19 -07:00
|
|
|
viewBindings?: any[],
|
2015-08-26 11:44:59 -07:00
|
|
|
changeDetection?: ChangeDetectionStrategy,
|
2015-07-07 22:09:19 +02:00
|
|
|
} = {}) {
|
|
|
|
super({
|
|
|
|
selector: selector,
|
|
|
|
properties: properties,
|
|
|
|
events: events,
|
|
|
|
host: host,
|
|
|
|
exportAs: exportAs,
|
2015-07-29 15:01:22 -07:00
|
|
|
bindings: bindings,
|
2015-07-07 22:09:19 +02:00
|
|
|
compileChildren: compileChildren
|
|
|
|
});
|
|
|
|
|
|
|
|
this.changeDetection = changeDetection;
|
2015-07-29 15:01:22 -07:00
|
|
|
this.viewBindings = viewBindings;
|
2015-07-07 22:09:19 +02:00
|
|
|
}
|
2015-06-12 23:51:42 -07:00
|
|
|
}
|
|
|
|
|
2015-08-07 11:41:38 -07:00
|
|
|
/**
|
|
|
|
* Declare reusable pipe function.
|
|
|
|
*
|
|
|
|
* ## Example
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @Pipe({
|
|
|
|
* name: 'lowercase'
|
|
|
|
* })
|
|
|
|
* class Lowercase {
|
|
|
|
* transform(v, args) { return v.toLowerCase(); }
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
@CONST()
|
2015-08-14 10:03:45 -07:00
|
|
|
export class PipeMetadata extends InjectableMetadata {
|
2015-08-07 11:41:38 -07:00
|
|
|
name: string;
|
|
|
|
|
|
|
|
constructor({name}: {name: string}) {
|
|
|
|
super();
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
}
|
2015-09-03 15:10:48 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Declare a bound field.
|
|
|
|
*
|
|
|
|
* ## Example
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @Directive({
|
|
|
|
* selector: 'sample-dir'
|
|
|
|
* })
|
|
|
|
* class SampleDir {
|
|
|
|
* @Property() property; // Same as @Property('property') property;
|
|
|
|
* @Property("el-property") dirProperty;
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
@CONST()
|
|
|
|
export class PropertyMetadata {
|
|
|
|
constructor(public bindingPropertyName?: string) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Declare a bound event.
|
|
|
|
*
|
|
|
|
* ## Example
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @Directive({
|
|
|
|
* selector: 'sample-dir'
|
|
|
|
* })
|
|
|
|
* class SampleDir {
|
|
|
|
* @Event() event = new EventEmitter(); // Same as @Event('event') event = new EventEmitter();
|
|
|
|
* @Event("el-event") dirEvent = new EventEmitter();
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
@CONST()
|
|
|
|
export class EventMetadata {
|
|
|
|
constructor(public bindingPropertyName?: string) {}
|
2015-09-04 14:07:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Declare a host property binding.
|
|
|
|
*
|
|
|
|
* ## Example
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @Directive({
|
|
|
|
* selector: 'sample-dir'
|
|
|
|
* })
|
|
|
|
* class SampleDir {
|
|
|
|
* @HostBinding() prop1; // Same as @HostBinding('prop1') prop1;
|
|
|
|
* @HostBinding("el-prop") prop2;
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* This is equivalent to
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @Directive({
|
|
|
|
* selector: 'sample-dir',
|
|
|
|
* host: {'[prop1]': 'prop1', '[el-prop]': 'prop2'}
|
|
|
|
* })
|
|
|
|
* class SampleDir {
|
|
|
|
* prop1;
|
|
|
|
* prop2;
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
@CONST()
|
|
|
|
export class HostBindingMetadata {
|
|
|
|
constructor(public hostPropertyName?: string) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Declare a host listener.
|
|
|
|
*
|
|
|
|
* ## Example
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @Directive({
|
|
|
|
* selector: 'sample-dir'
|
|
|
|
* })
|
|
|
|
* class SampleDir {
|
|
|
|
* @HostListener("change", ['$event.target.value']) onChange(value){}
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* This is equivalent to
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @Directive({
|
|
|
|
* selector: 'sample-dir',
|
|
|
|
* host: {'(change)': 'onChange($event.target.value)'}
|
|
|
|
* })
|
|
|
|
* class SampleDir {
|
|
|
|
* onChange(value){}
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
@CONST()
|
|
|
|
export class HostListenerMetadata {
|
|
|
|
constructor(public eventName: string, public args?: string[]) {}
|
2015-09-03 15:10:48 -07:00
|
|
|
}
|