feat(core): add support for @ContentChild and @ViewChild

Closes #4251
This commit is contained in:
vsavkin
2015-09-19 18:39:35 -07:00
committed by Victor Savkin
parent 2e9de0b169
commit c2a60f1624
8 changed files with 313 additions and 23 deletions

View File

@ -170,11 +170,13 @@ export class QueryMetadata extends DependencyMetadata {
* children (true).
*/
descendants: boolean;
first: boolean;
constructor(private _selector: Type | string,
{descendants = false}: {descendants?: boolean} = {}) {
{descendants = false, first = false}: {descendants?: boolean, first?: boolean} = {}) {
super();
this.descendants = descendants;
this.first = first;
}
/**
@ -229,6 +231,32 @@ export class ContentChildrenMetadata extends QueryMetadata {
}
}
// TODO: add an example after ContentChild and ViewChild are in master
/**
* Configures a content query.
*
* Content queries are set before the `afterContentInit` callback is called.
*
* ### Example
*
* ```
* @Directive({
* selector: 'someDir'
* })
* class SomeDir {
* @ContentChild(ChildDirective) contentChild;
*
* afterContentInit() {
* // contentChild is set
* }
* }
* ```
*/
@CONST()
export class ContentChildMetadata extends QueryMetadata {
constructor(_selector: Type | string) { super(_selector, {descendants: true, first: true}); }
}
/**
* Similar to {@link QueryMetadata}, but querying the component view, instead of
* the content children.
@ -266,8 +294,9 @@ export class ContentChildrenMetadata extends QueryMetadata {
*/
@CONST()
export class ViewQueryMetadata extends QueryMetadata {
constructor(_selector: Type | string, {descendants = false}: {descendants?: boolean} = {}) {
super(_selector, {descendants: descendants});
constructor(_selector: Type | string,
{descendants = false, first = false}: {descendants?: boolean, first?: boolean} = {}) {
super(_selector, {descendants: descendants, first: first});
}
/**
@ -302,3 +331,29 @@ export class ViewQueryMetadata extends QueryMetadata {
export class ViewChildrenMetadata extends ViewQueryMetadata {
constructor(_selector: Type | string) { super(_selector, {descendants: true}); }
}
/**
* Configures a view query.
*
* View queries are set before the `afterViewInit` callback is called.
*
* ### Example
*
* ```
* @Component({
* selector: 'someDir'
* })
* @View({templateUrl: 'someTemplate', directives: [ItemDirective]})
* class SomeDir {
* @ViewChild(ItemDirective) viewChild:ItemDirective;
*
* afterViewInit() {
* // viewChild is set
* }
* }
* ```
*/
@CONST()
export class ViewChildMetadata extends ViewQueryMetadata {
constructor(_selector: Type | string) { super(_selector, {descendants: true, first: true}); }
}