viewChild
Initializes a view child query.
const viewChild: ViewChildFunction;
Description
Consider using viewChild.required
for queries that should always match.
Further information is available in the Usage Notes...
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>
}