viewChild
Initializer API
Initializes a view child query.
Consider using viewChild.required
for queries that should always match.
function viewChild<LocatorT, ReadT>(locator: string | ProviderToken<LocatorT>, opts: { read: ProviderToken<ReadT>; debugName?: string | undefined; }): Signal<ReadT | undefined>;
Initializes a view child query. Consider using viewChild.required
for queries that should
always match.
@returns
Signal<ReadT | undefined>
function viewChild<LocatorT>(locator: string | ProviderToken<LocatorT>, opts?: { debugName?: string | undefined; } | undefined): Signal<LocatorT | undefined>;
@paramopts
{ debugName?: string | undefined; } | undefined
@returns
Signal<LocatorT | undefined>
function viewChild.required<LocatorT>(locator: string | ProviderToken<LocatorT>, opts?: { debugName?: string | undefined; } | undefined): Signal<LocatorT>;
@paramopts
{ debugName?: string | undefined; } | undefined
@returns
Signal<LocatorT>
function viewChild.required<LocatorT, ReadT>(locator: string | ProviderToken<LocatorT>, opts: { read: ProviderToken<ReadT>; debugName?: string | undefined; }): Signal<ReadT>;
@returns
Signal<ReadT>
Usage Notes
Create a child query in your component by declaring a
class field and initializing it with the viewChild()
function.
@Component({template: '<div #el></div><my-component #cmp />'})export class TestComponent { divEl = viewChild<ElementRef>('el'); // Signal<ElementRef|undefined> divElRequired = viewChild.required<ElementRef>('el'); // Signal<ElementRef> cmp = viewChild(MyComponent); // Signal<MyComponent|undefined> cmpRequired = viewChild.required(MyComponent); // Signal<MyComponent>}
Jump to details