Skip to content

Commit

Permalink
Revert "feat(common): add injector input to ngTemplateOutlet (#44761)" (
Browse files Browse the repository at this point in the history
#44807)

This reverts commit ed21f5c.

PR Close #44807
  • Loading branch information
crisbeto authored and AndrewKushnir committed Jan 24, 2022
1 parent 94bfcdd commit 40f27a3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 45 deletions.
3 changes: 1 addition & 2 deletions goldens/public-api/common/common.md
Original file line number Diff line number Diff line change
Expand Up @@ -581,9 +581,8 @@ export class NgTemplateOutlet implements OnChanges {
ngOnChanges(changes: SimpleChanges): void;
ngTemplateOutlet: TemplateRef<any> | null;
ngTemplateOutletContext: Object | null;
ngTemplateOutletInjector: Injector | null;
// (undocumented)
static ɵdir: i0.ɵɵDirectiveDeclaration<NgTemplateOutlet, "[ngTemplateOutlet]", never, { "ngTemplateOutletContext": "ngTemplateOutletContext"; "ngTemplateOutlet": "ngTemplateOutlet"; "ngTemplateOutletInjector": "ngTemplateOutletInjector"; }, {}, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<NgTemplateOutlet, "[ngTemplateOutlet]", never, { "ngTemplateOutletContext": "ngTemplateOutletContext"; "ngTemplateOutlet": "ngTemplateOutlet"; }, {}, never>;
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<NgTemplateOutlet, never>;
}
Expand Down
19 changes: 4 additions & 15 deletions packages/common/src/directives/ng_template_outlet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Directive, EmbeddedViewRef, Injector, Input, OnChanges, SimpleChanges, TemplateRef, ViewContainerRef} from '@angular/core';
import {Directive, EmbeddedViewRef, Input, OnChanges, SimpleChange, SimpleChanges, TemplateRef, ViewContainerRef} from '@angular/core';

/**
* @ngModule CommonModule
Expand Down Expand Up @@ -49,9 +49,6 @@ export class NgTemplateOutlet implements OnChanges {
*/
@Input() public ngTemplateOutlet: TemplateRef<any>|null = null;

/** Injector to be used within the embedded view. */
@Input() public ngTemplateOutletInjector: Injector|null = null;

constructor(private _viewContainerRef: ViewContainerRef) {}

ngOnChanges(changes: SimpleChanges) {
Expand All @@ -62,17 +59,9 @@ export class NgTemplateOutlet implements OnChanges {
viewContainerRef.remove(viewContainerRef.indexOf(this._viewRef));
}

if (this.ngTemplateOutlet) {
const {
ngTemplateOutlet: template,
ngTemplateOutletContext: context,
ngTemplateOutletInjector: injector
} = this;
this._viewRef = viewContainerRef.createEmbeddedView(
template, context, injector ? {injector} : undefined);
} else {
this._viewRef = null;
}
this._viewRef = this.ngTemplateOutlet ?
viewContainerRef.createEmbeddedView(this.ngTemplateOutlet, this.ngTemplateOutletContext) :
null;
} else if (
this._viewRef && changes['ngTemplateOutletContext'] && this.ngTemplateOutletContext) {
this._viewRef.context = this.ngTemplateOutletContext;
Expand Down
30 changes: 2 additions & 28 deletions packages/common/test/directives/ng_template_outlet_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import {CommonModule} from '@angular/common';
import {Component, ContentChildren, Directive, Inject, Injectable, InjectionToken, Injector, NO_ERRORS_SCHEMA, OnDestroy, QueryList, TemplateRef} from '@angular/core';
import {Component, ContentChildren, Directive, Injectable, NO_ERRORS_SCHEMA, OnDestroy, QueryList, TemplateRef} from '@angular/core';
import {ComponentFixture, TestBed, waitForAsync} from '@angular/core/testing';
import {expect} from '@angular/platform-browser/testing/src/matchers';

Expand All @@ -29,13 +29,7 @@ describe('NgTemplateOutlet', () => {

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
TestComponent,
CaptureTplRefs,
DestroyableCmpt,
MultiContextComponent,
InjectValueComponent,
],
declarations: [TestComponent, CaptureTplRefs, DestroyableCmpt, MultiContextComponent],
imports: [CommonModule],
providers: [DestroyedSpyService]
});
Expand Down Expand Up @@ -268,19 +262,8 @@ describe('NgTemplateOutlet', () => {
expect(componentInstance.context1).toEqual({name: 'two'});
expect(componentInstance.context2).toEqual({name: 'one'});
});

it('should be able to specify an injector', waitForAsync(() => {
const template = `<ng-template #tpl><inject-value></inject-value></ng-template>` +
`<ng-container *ngTemplateOutlet="tpl; injector: injector"></ng-container>`;
fixture = createTestComponent(template);
fixture.componentInstance.injector =
Injector.create({providers: [{provide: templateToken, useValue: 'world'}]});
detectChangesAndExpectText('Hello world');
}));
});

const templateToken = new InjectionToken<string>('templateToken');

@Injectable()
class DestroyedSpyService {
destroyed = false;
Expand All @@ -307,15 +290,6 @@ class TestComponent {
currentTplRef!: TemplateRef<any>;
context: any = {foo: 'bar'};
value = 'bar';
injector: Injector|null = null;
}

@Component({
selector: 'inject-value',
template: 'Hello {{tokenValue}}',
})
class InjectValueComponent {
constructor(@Inject(templateToken) public tokenValue: string) {}
}

@Component({
Expand Down

0 comments on commit 40f27a3

Please sign in to comment.