Declares a reference to a single child element.

The ViewChildren annotation takes an argument to select elements.

  • If the argument is a Type, a directive or a component with the type will be bound.

  • 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.

In either case, @ViewChild() assigns the first (looking from above) element if there are multiple matches.

View child is set before the ngAfterViewInit callback is called.

Example

With type selector:

@Component(
  selector: 'child-cmp',
  template: '<p>child</p>'
)
class ChildCmp {
  doSomething() {}
}
 
@Component(
  selector: 'some-cmp',
  template: '<child-cmp></child-cmp>',
  directives: const [ChildCmp]
)
class SomeCmp {
  @ViewChild(ChildCmp)
  ChildCmp child;
 
  @override
  ngAfterViewInit() {
    // child is set
    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: const [ChildCmp]
)
class SomeCmp {
  @ViewChild('child')
  ChildCmp child;
 
  @override
  ngAfterViewInit() {
    // child is set
    child.doSomething();
  }
}
Inheritance

Constructors

ViewChild(_selector, { read: null })

const

Properties

descendants → bool

whether we want to query only direct children (false) or all children (true).

read-only, inherited
first → bool

read-only, inherited
hashCode → int

Get a hash code for this object.

read-only, inherited
isVarBindingQuery → bool

Whether this is querying for a variable binding or a directive.

read-only, inherited
isViewQuery → dynamic

Always true to differentiate it with Query.

read-only, inherited
read → dynamic

The DI token to read from an element that matches the selector.

read-only, inherited
runtimeType → Type

A representation of the runtime type of the object.

read-only, inherited
selector → dynamic

read-only, inherited
token → dynamic

read-only, inherited
varBindings → List

A list of variable bindings this is querying for.

read-only, inherited

Operators

operator ==(other) → bool

The equality operator.

inherited

Methods

noSuchMethod(Invocation invocation) → dynamic

Invoked when a non-existent method or property is accessed.

inherited
toString() → String

Returns a string representation of this object.

inherited