feat(core): allow reading `Injector` from a view or content query by arturovt · Pull Request #70561 · angular/angular · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions adev/src/content/guide/components/queries.md
8 changes: 4 additions & 4 deletions packages/core/src/metadata/di.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export interface ContentChildrenDecorator {
* * Any provider defined on the injector of the component that is matched by the `selector` of
* this query
* * Any provider defined through a string token (e.g. `{provide: 'token', useValue: 'val'}`)
* * `TemplateRef`, `ElementRef`, and `ViewContainerRef`
* * `TemplateRef`, `ElementRef`, `ViewContainerRef`, and `Injector`
*
* @usageNotes
*
Expand Down Expand Up @@ -249,7 +249,7 @@ export interface ContentChildDecorator {
* * Any provider defined on the injector of the component that is matched by the `selector` of
* this query
* * Any provider defined through a string token (e.g. `{provide: 'token', useValue: 'val'}`)
* * `TemplateRef`, `ElementRef`, and `ViewContainerRef`
* * `TemplateRef`, `ElementRef`, `ViewContainerRef`, and `Injector`
*
* Difference between dynamic and static queries:
*
Expand Down Expand Up @@ -357,7 +357,7 @@ export interface ViewChildrenDecorator {
* * Any provider defined on the injector of the component that is matched by the `selector` of
* this query
* * Any provider defined through a string token (e.g. `{provide: 'token', useValue: 'val'}`)
* * `TemplateRef`, `ElementRef`, and `ViewContainerRef`
* * `TemplateRef`, `ElementRef`, `ViewContainerRef`, and `Injector`
*
* @usageNotes
*
Expand Down Expand Up @@ -444,7 +444,7 @@ export interface ViewChildDecorator {
* * Any provider defined on the injector of the component that is matched by the `selector` of
* this query
* * Any provider defined through a string token (e.g. `{provide: 'token', useValue: 'val'}`)
* * `TemplateRef`, `ElementRef`, and `ViewContainerRef`
* * `TemplateRef`, `ElementRef`, `ViewContainerRef`, and `Injector`
*
* Difference between dynamic and static queries:
* * Dynamic queries \(`static: false`\) - The query resolves before the `ngAfterViewInit()`
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/render3/queries/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// We are temporarily importing the existing viewEngine_from core so we can be sure we are
// correctly implementing its interfaces for backwards compatibility.

import {Injector} from '../../di/injector';
import {ProviderToken} from '../../di/provider_token';
import {createElementRef, ElementRef as ViewEngine_ElementRef} from '../../linker/element_ref';
import {QueryList} from '../../linker/query_list';
Expand All @@ -18,7 +19,7 @@ import {assertDefined, assertIndexInRange, assertNumber, throwError} from '../..
import {stringify} from '../../util/stringify';

import {assertFirstCreatePass, assertLContainer} from '../assert';
import {getNodeInjectable, locateDirectiveOrProvider} from '../di';
import {getNodeInjectable, locateDirectiveOrProvider, NodeInjector} from '../di';
import {CONTAINER_HEADER_OFFSET, LContainer, MOVED_VIEWS} from '../interfaces/container';
import {
TContainerNode,
Expand Down Expand Up @@ -287,6 +288,7 @@ class TQuery_ implements TQuery {
if (
read === ViewEngine_ElementRef ||
read === ViewContainerRef ||
read === Injector ||
(read === ViewEngine_TemplateRef && tNode.type & TNodeType.Container)
) {
this.addMatch(tNode.index, -2);
Expand Down Expand Up @@ -370,10 +372,12 @@ function createSpecialToken(lView: LView, tNode: TNode, read: any): any {
tNode as TElementNode | TContainerNode | TElementContainerNode,
lView,
);
} else if (read === Injector) {
return new NodeInjector(tNode as TElementNode | TContainerNode | TElementContainerNode, lView);
} else {
ngDevMode &&
throwError(
`Special token to read should be one of ElementRef, TemplateRef or ViewContainerRef but got ${stringify(
`Special token to read should be one of ElementRef, TemplateRef, ViewContainerRef or Injector but got ${stringify(
read,
)}.`,
);
Expand Down
29 changes: 29 additions & 0 deletions packages/core/test/acceptance/authoring/signal_queries_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import {
Directive,
ElementRef,
EnvironmentInjector,
inject,
InjectionToken,
Injector,
QueryList,
ViewChild,
viewChild,
Expand Down Expand Up @@ -270,6 +273,32 @@ describe('queries as signals', () => {
const node = fixture.componentInstance.viewChildQuery![SIGNAL] as {debugName: string};
expect(node.debugName).toBe('viewChildQuery');
});

it('should read the node injector of the queried element with `read: Injector`', () => {
const TOKEN = new InjectionToken<string>('TOKEN');

@Directive({
selector: '[provider]',
providers: [{provide: TOKEN, useValue: 'from directive'}],
})
class ProviderDir {}

@Component({
imports: [ProviderDir],
template: `<div #el provider></div>`,
})
class AppComponent {
elInjector = viewChild('el', {read: Injector});
ownInjector = inject(Injector);
}

const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();

const queried = fixture.componentInstance.elInjector()!;
expect(queried.get(TOKEN)).toBe('from directive');
expect(fixture.componentInstance.ownInjector.get(TOKEN, null)).toBeNull();
});
});

describe('content queries', () => {
Expand Down
51 changes: 51 additions & 0 deletions packages/core/test/acceptance/query_spec.ts