viewChild
Инициализирует запрос дочернего элемента представления.
Для запросов, которые всегда должны находить соответствие, рассмотрите возможность использования viewChild.required.
API
function viewChild<LocatorT, ReadT>(locator, opts); function viewChild<LocatorT>(locator, opts?); function viewChild.required<LocatorT>(locator, opts?); function viewChild.required<LocatorT, ReadT>(locator, opts);
function viewChild<LocatorT, ReadT>(locator: string | ProviderToken<LocatorT>, opts: { read: ProviderToken<ReadT>; debugName?: string | undefined; }): Signal<ReadT | undefined>;Инициализирует запрос дочернего элемента представления. Для запросов, которые всегда должны находить соответствие, рассмотрите возможность использования viewChild.required.
@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>
Примечания по использованию
Создайте запрос дочернего элемента в компоненте, объявив поле класса и инициализировав его функцией viewChild().
@Component({template: '<div #el></div><my-component #cmp />'})
export class Card {
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>
}
Super-powered by Google ©2010–2025.
Code licensed under an MIT-style License. Documentation licensed under CC BY 4.0.
https://angular.dev/api/core/viewChild