Skip to content

Commit

Permalink
feat(core): support flags option in NodeInjector.get
Browse files Browse the repository at this point in the history
close #31776

the InectFlags was defined in both getOrCreateInjectable and Injector, but was ignored in NodeInjector
this PR support getting parent token via injector.get in NodeInjector
  • Loading branch information
vthinkxie committed Apr 29, 2021
1 parent 55d9713 commit 69448a4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/di/injector.ts
Expand Up @@ -71,7 +71,7 @@ export abstract class Injector {
* @deprecated from v4.0.0 use ProviderToken<T>
* @suppress {duplicate}
*/
abstract get(token: any, notFoundValue?: any): any;
abstract get(token: any, notFoundValue?: any, flags?: InjectFlags): any;

/**
* @deprecated from v5 use the new signature Injector.create(options)
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/render3/di.ts
Expand Up @@ -688,8 +688,8 @@ export class NodeInjector implements Injector {
private _tNode: TElementNode|TContainerNode|TElementContainerNode|null,
private _lView: LView) {}

get(token: any, notFoundValue?: any): any {
return getOrCreateInjectable(this._tNode, this._lView, token, undefined, notFoundValue);
get(token: any, notFoundValue?: any, flags?: InjectFlags): any {
return getOrCreateInjectable(this._tNode, this._lView, token, flags, notFoundValue);
}
}

Expand Down
40 changes: 39 additions & 1 deletion packages/core/test/acceptance/di_spec.ts
Expand Up @@ -7,7 +7,7 @@
*/

import {CommonModule} from '@angular/common';
import {Attribute, ChangeDetectorRef, Component, ComponentFactoryResolver, ComponentRef, ContentChild, Directive, ElementRef, EventEmitter, forwardRef, Host, HostBinding, Inject, Injectable, InjectionToken, INJECTOR, Injector, Input, LOCALE_ID, NgModule, NgZone, Optional, Output, Pipe, PipeTransform, Self, SkipSelf, TemplateRef, ViewChild, ViewContainerRef, ViewRef, ɵDEFAULT_LOCALE_ID as DEFAULT_LOCALE_ID} from '@angular/core';
import {Attribute, ChangeDetectorRef, Component, ComponentFactoryResolver, ComponentRef, ContentChild, Directive, ElementRef, EventEmitter, forwardRef, Host, HostBinding, Inject, Injectable, InjectFlags, InjectionToken, INJECTOR, Injector, Input, LOCALE_ID, NgModule, NgZone, Optional, Output, Pipe, PipeTransform, Self, SkipSelf, TemplateRef, ViewChild, ViewContainerRef, ViewRef, ɵDEFAULT_LOCALE_ID as DEFAULT_LOCALE_ID} from '@angular/core';
import {ɵINJECTOR_SCOPE} from '@angular/core/src/core';
import {ViewRef as ViewRefInternal} from '@angular/core/src/render3/view_ref';
import {TestBed} from '@angular/core/testing';
Expand Down Expand Up @@ -1919,6 +1919,44 @@ describe('di', () => {
});
});
});

onlyInIvy('Ivy support InjectFlags in Injector')
.it('should support SkipSelf flag in Injector', () => {
@Component({
selector: 'parent',
template: '<child></child>',
providers: [{
provide: 'token',
useValue: 'PARENT',
}]
})
class ParentComponent {
}

@Component({
selector: 'child',
template: '...',
providers: [{
provide: 'token',
useValue: 'CHILD',
}]
})
class ChildComponent {
constructor(public injector: Injector, @SkipSelf() public parentInjector: Injector) {}
}

TestBed.configureTestingModule({
declarations: [ParentComponent, ChildComponent],
});
const fixture = TestBed.createComponent(ParentComponent);
fixture.detectChanges();

const childComponent =
fixture.debugElement.query(By.directive(ChildComponent)).componentInstance;
expect(childComponent.injector.get('token')).toBe('CHILD');
expect(childComponent.injector.get('token', null, InjectFlags.SkipSelf)).toBe('PARENT');
expect(childComponent.parentInjector.get('token')).toBe('PARENT');
});
});
});

Expand Down

0 comments on commit 69448a4

Please sign in to comment.