NG.I · Angular 22 practice question
A developer is working on a custom host component that needs to pass the specific dependency injection context of a projected child element to an outlet template. Which approach, using `contentChild`, allows them to retrieve the injector associated with that projected element?
export class CustomHost {
childInjector = contentChild(TargetDirective, {read: Injector});
}
Check answer and explanation
Correct answer: Using `contentChild(TargetDirective, {read: Injector})` to capture the injector from the element matching `TargetDirective`.
Explanation
The official documentation indicates that supplying `Injector` within the `read` property enables the retrieval of the injector associated with the element that matches the query. This concept is illustrated by the example showing the use of `contentChild(TemplateRef)` alongside `innerInjector = viewChild('inner', {read: Injector})`.
Common trap
The assertion that `viewChild` is the appropriate method for querying projected content is flawed, as is the approach of manually navigating the DOM, which circumvents Angular's built-in structured querying capabilities.
Source: Angular docs: Referencing component children with queries — Reading specific values from an element's injector
Checked against the cited text on 2026-11-25.