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-04-28 17:50:03 -07:00
|
|
|
import {resolveForwardRef} from '../di/forward_ref';
|
2016-07-07 10:05:55 -07:00
|
|
|
import {OpaqueToken} from '../di/opaque_token';
|
2016-08-10 18:21:28 -07:00
|
|
|
import {StringWrapper, isString, stringify} from '../facade/lang';
|
|
|
|
|
import {Type} from '../type';
|
2016-09-12 09:44:20 -07:00
|
|
|
import {makeParamDecorator, makePropDecorator} from '../util/decorators';
|
2016-06-08 16:38:52 -07:00
|
|
|
|
2016-07-07 10:05:55 -07:00
|
|
|
/**
|
|
|
|
|
* This token can be used to create a virtual provider that will populate the
|
2016-07-25 00:36:30 -07:00
|
|
|
* `entryComponents` fields of components and ng modules based on its `useValue`.
|
2016-07-07 10:05:55 -07:00
|
|
|
* All components that are referenced in the `useValue` value (either directly
|
2016-07-25 00:36:30 -07:00
|
|
|
* or in a nested array or map) will be added to the `entryComponents` property.
|
2016-07-07 10:05:55 -07:00
|
|
|
*
|
|
|
|
|
* ### Example
|
2016-07-25 00:36:30 -07:00
|
|
|
* The following example shows how the router can populate the `entryComponents`
|
2016-07-18 03:50:31 -07:00
|
|
|
* field of an NgModule based on the router configuration which refers
|
2016-07-07 10:05:55 -07:00
|
|
|
* to components.
|
|
|
|
|
*
|
|
|
|
|
* ```typescript
|
|
|
|
|
* // helper function inside the router
|
|
|
|
|
* function provideRoutes(routes) {
|
|
|
|
|
* return [
|
|
|
|
|
* {provide: ROUTES, useValue: routes},
|
2016-07-25 00:36:30 -07:00
|
|
|
* {provide: ANALYZE_FOR_ENTRY_COMPONENTS, useValue: routes, multi: true}
|
2016-07-07 10:05:55 -07:00
|
|
|
* ];
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* // user code
|
|
|
|
|
* let routes = [
|
|
|
|
|
* {path: '/root', component: RootComp},
|
2016-09-05 21:37:21 +08:00
|
|
|
* {path: '/teams', component: TeamsComp}
|
2016-07-07 10:05:55 -07:00
|
|
|
* ];
|
|
|
|
|
*
|
2016-07-18 03:50:31 -07:00
|
|
|
* @NgModule({
|
2016-07-07 10:05:55 -07:00
|
|
|
* providers: [provideRoutes(routes)]
|
|
|
|
|
* })
|
|
|
|
|
* class ModuleWithRoutes {}
|
|
|
|
|
* ```
|
|
|
|
|
*
|
|
|
|
|
* @experimental
|
|
|
|
|
*/
|
2016-07-25 00:36:30 -07:00
|
|
|
export const ANALYZE_FOR_ENTRY_COMPONENTS = new OpaqueToken('AnalyzeForEntryComponents');
|
2015-01-16 15:30:22 -08:00
|
|
|
|
2016-09-12 09:44:20 -07:00
|
|
|
|
2015-03-25 09:42:19 +01:00
|
|
|
/**
|
2016-09-12 09:44:20 -07:00
|
|
|
* Type of the Attribute decorator / constructor function.
|
|
|
|
|
*
|
|
|
|
|
* @stable
|
|
|
|
|
*/
|
2016-09-12 19:14:17 -07:00
|
|
|
export interface AttributeDecorator {
|
2016-09-12 09:44:20 -07:00
|
|
|
/**
|
2015-04-14 23:26:49 +00:00
|
|
|
* Specifies that a constant attribute value should be injected.
|
|
|
|
|
*
|
|
|
|
|
* The directive can inject constant string literals of host element attributes.
|
|
|
|
|
*
|
2015-10-19 15:37:32 +01:00
|
|
|
* ### Example
|
2015-04-14 23:26:49 +00:00
|
|
|
*
|
2015-04-15 22:35:38 +00:00
|
|
|
* Suppose we have an `<input>` element and want to know its `type`.
|
2015-04-14 23:26:49 +00:00
|
|
|
*
|
|
|
|
|
* ```html
|
|
|
|
|
* <input type="text">
|
|
|
|
|
* ```
|
|
|
|
|
*
|
2015-04-15 22:35:38 +00:00
|
|
|
* A decorator can inject string literal `text` like so:
|
2015-04-14 23:26:49 +00:00
|
|
|
*
|
2015-11-30 08:28:54 -08:00
|
|
|
* {@example core/ts/metadata/metadata.ts region='attributeMetadata'}
|
2015-09-17 22:33:51 -07:00
|
|
|
*
|
2016-09-12 09:44:20 -07:00
|
|
|
* ### Example as TypeScript Decorator
|
2015-09-17 22:33:51 -07:00
|
|
|
*
|
2016-09-12 09:44:20 -07:00
|
|
|
* {@example core/ts/metadata/metadata.ts region='attributeFactory'}
|
2015-09-17 22:33:51 -07:00
|
|
|
*
|
2016-09-12 09:44:20 -07:00
|
|
|
* ### Example as ES5 DSL
|
2015-09-17 22:33:51 -07:00
|
|
|
*
|
|
|
|
|
* ```
|
2016-09-12 09:44:20 -07:00
|
|
|
* var MyComponent = ng
|
|
|
|
|
* .Component({...})
|
|
|
|
|
* .Class({
|
|
|
|
|
* constructor: [new ng.Attribute('title'), function(title) {
|
|
|
|
|
* ...
|
|
|
|
|
* }]
|
|
|
|
|
* })
|
2015-09-17 22:33:51 -07:00
|
|
|
* ```
|
|
|
|
|
*
|
2016-09-12 09:44:20 -07:00
|
|
|
* ### Example as ES5 annotation
|
2015-09-17 22:33:51 -07:00
|
|
|
*
|
|
|
|
|
* ```
|
2016-09-12 09:44:20 -07:00
|
|
|
* var MyComponent = function(title) {
|
|
|
|
|
* ...
|
|
|
|
|
* };
|
|
|
|
|
*
|
|
|
|
|
* MyComponent.annotations = [
|
|
|
|
|
* new ng.Component({...})
|
|
|
|
|
* ]
|
|
|
|
|
* MyComponent.parameters = [
|
|
|
|
|
* [new ng.Attribute('title')]
|
|
|
|
|
* ]
|
2015-09-17 22:33:51 -07:00
|
|
|
* ```
|
|
|
|
|
*
|
2016-09-12 09:44:20 -07:00
|
|
|
* @stable
|
|
|
|
|
*/ (name: string): any;
|
|
|
|
|
new (name: string): Attribute;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Type of the Attribute metadata.
|
2015-09-17 22:33:51 -07:00
|
|
|
*
|
2016-09-12 09:44:20 -07:00
|
|
|
* @stable
|
|
|
|
|
*/
|
|
|
|
|
export interface Attribute { attributeName?: string; }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Attribute decorator and metadata.
|
2015-09-17 22:33:51 -07:00
|
|
|
*
|
2016-09-12 09:44:20 -07:00
|
|
|
* @stable
|
|
|
|
|
* @Annotation
|
|
|
|
|
*/
|
2016-09-12 19:14:17 -07:00
|
|
|
export const Attribute: AttributeDecorator = makeParamDecorator([['attributeName', undefined]]);
|
2016-09-12 09:44:20 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Type of the Query metadata.
|
2015-09-17 22:33:51 -07:00
|
|
|
*
|
2016-08-15 16:07:55 -07:00
|
|
|
* @stable
|
2015-03-13 11:39:15 -07:00
|
|
|
*/
|
2016-09-12 09:44:20 -07:00
|
|
|
export interface Query {
|
2015-06-10 17:08:22 -07:00
|
|
|
descendants: boolean;
|
2015-09-19 18:39:35 -07:00
|
|
|
first: boolean;
|
2016-04-18 13:24:42 -07:00
|
|
|
read: any;
|
2016-09-12 09:44:20 -07:00
|
|
|
isViewQuery: boolean;
|
|
|
|
|
selector: any;
|
|
|
|
|
}
|
2015-09-17 22:33:51 -07:00
|
|
|
|
2016-09-12 19:34:14 -07:00
|
|
|
/**
|
|
|
|
|
* Base class for query metadata
|
|
|
|
|
*
|
|
|
|
|
* @stable
|
|
|
|
|
*/
|
2016-09-12 09:44:20 -07:00
|
|
|
export abstract class Query {}
|
2015-06-10 17:08:22 -07:00
|
|
|
|
2016-09-12 09:44:20 -07:00
|
|
|
/**
|
|
|
|
|
* Type of the ContentChildren decorator / constructor function.
|
|
|
|
|
*
|
|
|
|
|
* @stable
|
|
|
|
|
*/
|
2016-09-12 19:14:17 -07:00
|
|
|
export interface ContentChildrenDecorator {
|
2015-09-17 22:33:51 -07:00
|
|
|
/**
|
2016-09-12 09:44:20 -07:00
|
|
|
* Configures a content query.
|
|
|
|
|
*
|
|
|
|
|
* Content queries are set before the `ngAfterContentInit` callback is called.
|
|
|
|
|
*
|
|
|
|
|
* ### Example
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
* @Directive({
|
|
|
|
|
* selector: 'someDir'
|
|
|
|
|
* })
|
|
|
|
|
* class SomeDir {
|
|
|
|
|
* @ContentChildren(ChildDirective) contentChildren: QueryList<ChildDirective>;
|
|
|
|
|
*
|
|
|
|
|
* ngAfterContentInit() {
|
|
|
|
|
* // contentChildren is set
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* ```
|
|
|
|
|
* @stable
|
2015-09-17 22:33:51 -07:00
|
|
|
*/
|
2016-09-12 09:44:20 -07:00
|
|
|
(selector: Type<any>|Function|string,
|
|
|
|
|
{descendants, read}?: {descendants?: boolean, read?: any}): any;
|
|
|
|
|
new (
|
|
|
|
|
selector: Type<any>|Function|string,
|
|
|
|
|
{descendants, read}?: {descendants?: boolean, read?: any}): Query;
|
|
|
|
|
}
|
2015-07-10 10:30:31 -07:00
|
|
|
|
2016-09-12 09:44:20 -07:00
|
|
|
/**
|
|
|
|
|
* Type of the ContentChildren metadata.
|
|
|
|
|
*
|
|
|
|
|
* @stable
|
|
|
|
|
*/
|
|
|
|
|
export type ContentChildren = Query;
|
2015-06-10 17:08:22 -07:00
|
|
|
|
2016-09-12 09:44:20 -07:00
|
|
|
/**
|
|
|
|
|
* ContentChildren decorator and metadata.
|
|
|
|
|
*
|
|
|
|
|
* @stable
|
|
|
|
|
* @Annotation
|
|
|
|
|
*/
|
2016-09-12 19:14:17 -07:00
|
|
|
export const ContentChildren: ContentChildrenDecorator = makePropDecorator(
|
2016-09-12 09:44:20 -07:00
|
|
|
[
|
|
|
|
|
['selector', undefined],
|
|
|
|
|
{first: false, isViewQuery: false, descendants: false, read: undefined}
|
|
|
|
|
],
|
|
|
|
|
Query);
|
2015-06-15 15:18:11 -07:00
|
|
|
|
2016-09-12 09:44:20 -07:00
|
|
|
/**
|
|
|
|
|
* Type of the ContentChild decorator / constructor function.
|
|
|
|
|
*
|
|
|
|
|
* @stable
|
|
|
|
|
*/
|
2016-09-12 19:14:17 -07:00
|
|
|
export interface ContentChildDecorator {
|
2015-09-17 22:33:51 -07:00
|
|
|
/**
|
2016-09-12 09:44:20 -07:00
|
|
|
* Configures a content query.
|
|
|
|
|
*
|
|
|
|
|
* Content queries are set before the `ngAfterContentInit` callback is called.
|
|
|
|
|
*
|
|
|
|
|
* ### Example
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
* @Directive({
|
|
|
|
|
* selector: 'someDir'
|
|
|
|
|
* })
|
|
|
|
|
* class SomeDir {
|
|
|
|
|
* @ContentChild(ChildDirective) contentChild;
|
|
|
|
|
* @ContentChild('container_ref') containerChild
|
|
|
|
|
*
|
|
|
|
|
* ngAfterContentInit() {
|
|
|
|
|
* // contentChild is set
|
|
|
|
|
* // containerChild is set
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* ```
|
|
|
|
|
*
|
|
|
|
|
* ```html
|
|
|
|
|
* <container #container_ref>
|
|
|
|
|
* <item>a</item>
|
|
|
|
|
* <item>b</item>
|
|
|
|
|
* </container>
|
|
|
|
|
* ```
|
2015-09-17 22:33:51 -07:00
|
|
|
*/
|
2016-09-12 09:44:20 -07:00
|
|
|
(selector: Type<any>|Function|string, {read}?: {read?: any}): any;
|
|
|
|
|
new (selector: Type<any>|Function|string, {read}?: {read?: any}): ContentChild;
|
2015-03-13 11:39:15 -07:00
|
|
|
}
|
2015-07-10 10:30:31 -07:00
|
|
|
|
2015-09-17 18:45:14 -07:00
|
|
|
/**
|
2016-09-12 09:44:20 -07:00
|
|
|
* Type of the ContentChild metadata.
|
2015-09-17 18:45:14 -07:00
|
|
|
*
|
2016-05-25 15:00:05 -07:00
|
|
|
* @stable
|
2015-09-17 18:45:14 -07:00
|
|
|
*/
|
2016-09-12 09:44:20 -07:00
|
|
|
export type ContentChild = Query;
|
2015-09-17 18:45:14 -07:00
|
|
|
|
2015-09-19 18:39:35 -07:00
|
|
|
/**
|
2016-09-12 09:44:20 -07:00
|
|
|
* ContentChild decorator and metadata.
|
2015-09-19 18:39:35 -07:00
|
|
|
*
|
2016-05-25 15:00:05 -07:00
|
|
|
* @stable
|
2016-09-12 09:44:20 -07:00
|
|
|
* @Annotation
|
2015-09-19 18:39:35 -07:00
|
|
|
*/
|
2016-09-12 19:14:17 -07:00
|
|
|
export const ContentChild: ContentChildDecorator = makePropDecorator(
|
2016-09-12 09:44:20 -07:00
|
|
|
[
|
|
|
|
|
['selector', undefined], {
|
|
|
|
|
first: true,
|
|
|
|
|
isViewQuery: false,
|
|
|
|
|
descendants: false,
|
|
|
|
|
read: undefined,
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
Query);
|
2015-09-19 18:39:35 -07:00
|
|
|
|
2015-07-10 10:30:31 -07:00
|
|
|
/**
|
2016-09-12 19:14:17 -07:00
|
|
|
* Type of the ViewChildren decorator / constructor function.
|
2015-09-17 22:33:51 -07:00
|
|
|
*
|
2016-08-15 16:07:55 -07:00
|
|
|
* @stable
|
2015-07-10 10:30:31 -07:00
|
|
|
*/
|
2016-09-12 19:14:17 -07:00
|
|
|
export interface ViewChildrenDecorator {
|
2015-09-17 22:33:51 -07:00
|
|
|
/**
|
2016-02-19 15:31:27 +09:00
|
|
|
* Declares a list of child element references.
|
2015-09-17 18:45:14 -07:00
|
|
|
*
|
2016-04-13 16:03:30 -07:00
|
|
|
* Angular automatically updates the list when the DOM is updated.
|
2016-02-19 15:31:27 +09:00
|
|
|
*
|
|
|
|
|
* `ViewChildren` takes an argument to select elements.
|
|
|
|
|
*
|
|
|
|
|
* - If the argument is a type, directives or components with the type will be bound.
|
|
|
|
|
*
|
2016-04-13 16:03:30 -07:00
|
|
|
* - If the argument is a string, the string is interpreted as a list of comma-separated selectors.
|
|
|
|
|
* For each selector, an element containing the matching template variable (e.g. `#child`) will be
|
|
|
|
|
* bound.
|
2016-02-19 15:31:27 +09:00
|
|
|
*
|
|
|
|
|
* View children are set before the `ngAfterViewInit` callback is called.
|
2015-09-17 18:45:14 -07:00
|
|
|
*
|
|
|
|
|
* ### Example
|
|
|
|
|
*
|
2016-02-19 15:31:27 +09:00
|
|
|
* With type selector:
|
|
|
|
|
*
|
2015-09-17 18:45:14 -07:00
|
|
|
* ```
|
|
|
|
|
* @Component({
|
2016-02-19 15:31:27 +09:00
|
|
|
* selector: 'child-cmp',
|
|
|
|
|
* template: '<p>child</p>'
|
2015-09-17 18:45:14 -07:00
|
|
|
* })
|
2016-02-19 15:31:27 +09:00
|
|
|
* class ChildCmp {
|
|
|
|
|
* doSomething() {}
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* @Component({
|
|
|
|
|
* selector: 'some-cmp',
|
|
|
|
|
* template: `
|
|
|
|
|
* <child-cmp></child-cmp>
|
|
|
|
|
* <child-cmp></child-cmp>
|
|
|
|
|
* <child-cmp></child-cmp>
|
|
|
|
|
* `,
|
|
|
|
|
* directives: [ChildCmp]
|
|
|
|
|
* })
|
|
|
|
|
* class SomeCmp {
|
|
|
|
|
* @ViewChildren(ChildCmp) children:QueryList<ChildCmp>;
|
|
|
|
|
*
|
|
|
|
|
* ngAfterViewInit() {
|
|
|
|
|
* // children are set
|
|
|
|
|
* this.children.toArray().forEach((child)=>child.doSomething());
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* ```
|
|
|
|
|
*
|
|
|
|
|
* With string selector:
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
* @Component({
|
|
|
|
|
* selector: 'child-cmp',
|
|
|
|
|
* template: '<p>child</p>'
|
|
|
|
|
* })
|
|
|
|
|
* class ChildCmp {
|
|
|
|
|
* doSomething() {}
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* @Component({
|
|
|
|
|
* selector: 'some-cmp',
|
|
|
|
|
* template: `
|
|
|
|
|
* <child-cmp #child1></child-cmp>
|
|
|
|
|
* <child-cmp #child2></child-cmp>
|
|
|
|
|
* <child-cmp #child3></child-cmp>
|
|
|
|
|
* `,
|
|
|
|
|
* directives: [ChildCmp]
|
|
|
|
|
* })
|
|
|
|
|
* class SomeCmp {
|
|
|
|
|
* @ViewChildren('child1,child2,child3') children:QueryList<ChildCmp>;
|
2015-09-17 18:45:14 -07:00
|
|
|
*
|
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() {
|
2016-02-19 15:31:27 +09:00
|
|
|
* // children are set
|
|
|
|
|
* this.children.toArray().forEach((child)=>child.doSomething());
|
2015-09-17 18:45:14 -07:00
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* ```
|
2016-05-25 15:00:05 -07:00
|
|
|
* @stable
|
2016-09-12 09:44:20 -07:00
|
|
|
*/ (selector: Type<any>|Function|string, {read}?: {read?: any}): any;
|
|
|
|
|
new (selector: Type<any>|Function|string, {read}?: {read?: any}): ViewChildren;
|
2015-09-17 18:45:14 -07:00
|
|
|
}
|
2015-09-19 18:39:35 -07:00
|
|
|
|
|
|
|
|
/**
|
2016-09-12 09:44:20 -07:00
|
|
|
* Type of the ViewChildren metadata.
|
|
|
|
|
*
|
|
|
|
|
* @stable
|
|
|
|
|
*/
|
|
|
|
|
export type ViewChildren = Query;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ViewChildren decorator and metadata.
|
|
|
|
|
*
|
|
|
|
|
* @stable
|
|
|
|
|
* @Annotation
|
|
|
|
|
*/
|
2016-09-12 19:14:17 -07:00
|
|
|
export const ViewChildren: ViewChildrenDecorator = makePropDecorator(
|
2016-09-12 09:44:20 -07:00
|
|
|
[
|
|
|
|
|
['selector', undefined], {
|
|
|
|
|
first: false,
|
|
|
|
|
isViewQuery: true,
|
|
|
|
|
descendants: true,
|
|
|
|
|
read: undefined,
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
Query);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-09-12 19:14:17 -07:00
|
|
|
* Type of the ViewChild decorator / constructor function.
|
2016-09-12 09:44:20 -07:00
|
|
|
*
|
|
|
|
|
* @stable
|
|
|
|
|
*/
|
2016-09-12 19:14:17 -07:00
|
|
|
export interface ViewChildDecorator {
|
2016-09-12 09:44:20 -07:00
|
|
|
/**
|
2015-09-19 18:39:35 -07:00
|
|
|
*
|
2016-02-19 15:31:27 +09:00
|
|
|
* Declares a reference of child element.
|
|
|
|
|
*
|
|
|
|
|
* `ViewChildren` takes an argument to select elements.
|
|
|
|
|
*
|
|
|
|
|
* - If the argument is a type, a directive or a component with the type will be bound.
|
|
|
|
|
*
|
2016-09-01 11:45:59 -07:00
|
|
|
* If the argument is a string, the string is interpreted as a selector. An element containing the
|
|
|
|
|
* matching template variable (e.g. `#child`) will be bound.
|
2016-02-19 15:31:27 +09:00
|
|
|
*
|
2016-04-13 16:03:30 -07:00
|
|
|
* In either case, `@ViewChild()` assigns the first (looking from above) element if there are
|
|
|
|
|
multiple matches.
|
2016-02-19 15:31:27 +09:00
|
|
|
*
|
|
|
|
|
* View child is set before the `ngAfterViewInit` callback is called.
|
2015-09-19 18:39:35 -07:00
|
|
|
*
|
|
|
|
|
* ### Example
|
|
|
|
|
*
|
2016-02-19 15:31:27 +09:00
|
|
|
* With type selector:
|
|
|
|
|
*
|
2015-09-19 18:39:35 -07:00
|
|
|
* ```
|
|
|
|
|
* @Component({
|
2016-02-19 15:31:27 +09:00
|
|
|
* selector: 'child-cmp',
|
|
|
|
|
* template: '<p>child</p>'
|
2015-09-19 18:39:35 -07:00
|
|
|
* })
|
2016-02-19 15:31:27 +09:00
|
|
|
* class ChildCmp {
|
|
|
|
|
* doSomething() {}
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* @Component({
|
|
|
|
|
* selector: 'some-cmp',
|
|
|
|
|
* template: '<child-cmp></child-cmp>',
|
|
|
|
|
* directives: [ChildCmp]
|
|
|
|
|
* })
|
|
|
|
|
* class SomeCmp {
|
|
|
|
|
* @ViewChild(ChildCmp) child:ChildCmp;
|
|
|
|
|
*
|
|
|
|
|
* ngAfterViewInit() {
|
|
|
|
|
* // child is set
|
|
|
|
|
* this.child.doSomething();
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* ```
|
|
|
|
|
*
|
|
|
|
|
* With string selector:
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
* @Component({
|
|
|
|
|
* selector: 'child-cmp',
|
|
|
|
|
* template: '<p>child</p>'
|
|
|
|
|
* })
|
|
|
|
|
* class ChildCmp {
|
|
|
|
|
* doSomething() {}
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* @Component({
|
|
|
|
|
* selector: 'some-cmp',
|
|
|
|
|
* template: '<child-cmp #child></child-cmp>',
|
|
|
|
|
* directives: [ChildCmp]
|
|
|
|
|
* })
|
|
|
|
|
* class SomeCmp {
|
|
|
|
|
* @ViewChild('child') child:ChildCmp;
|
2015-09-19 18:39:35 -07:00
|
|
|
*
|
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() {
|
2016-02-19 15:31:27 +09:00
|
|
|
* // child is set
|
|
|
|
|
* this.child.doSomething();
|
2015-09-19 18:39:35 -07:00
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* ```
|
2016-05-25 15:00:05 -07:00
|
|
|
* @stable
|
2016-09-12 09:44:20 -07:00
|
|
|
*/ (selector: Type<any>|Function|string, {read}?: {read?: any}): any;
|
|
|
|
|
new (selector: Type<any>|Function|string, {read}?: {read?: any}): ViewChild;
|
2015-10-11 07:41:19 -07:00
|
|
|
}
|
2016-09-12 09:44:20 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Type of the ViewChild metadata.
|
|
|
|
|
*
|
|
|
|
|
* @stable
|
|
|
|
|
*/
|
|
|
|
|
export type ViewChild = Query;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ViewChild decorator and metadata.
|
|
|
|
|
*
|
|
|
|
|
* @stable
|
|
|
|
|
* @Annotation
|
|
|
|
|
*/
|
2016-09-12 19:14:17 -07:00
|
|
|
export const ViewChild: ViewChildDecorator = makePropDecorator(
|
2016-09-12 09:44:20 -07:00
|
|
|
[
|
|
|
|
|
['selector', undefined], {
|
|
|
|
|
first: true,
|
|
|
|
|
isViewQuery: true,
|
|
|
|
|
descendants: true,
|
|
|
|
|
read: undefined,
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
Query);
|