Skip to content

Answer:4 [Type-context-outlet] #1346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
5 changes: 3 additions & 2 deletions apps/angular/4-typed-context-outlet/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { NgTemplateOutlet } from '@angular/common';
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { ListComponent } from './list.component';
import { PersonComponent } from './person.component';
import { PersonDirective } from './person.directive';

@Component({
imports: [NgTemplateOutlet, PersonComponent, ListComponent],
imports: [NgTemplateOutlet, PersonComponent, ListComponent, PersonDirective],
selector: 'app-root',
template: `
<person [person]="person">
<ng-template #personRef let-name let-age="age">
<ng-template #personRef let-name let-age="age" person>
{{ name }}: {{ age }}
</ng-template>
</person>
Expand Down
12 changes: 7 additions & 5 deletions apps/angular/4-typed-context-outlet/src/app/person.component.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { NgTemplateOutlet } from '@angular/common';
import { CommonModule, NgTemplateOutlet } from '@angular/common';
import { Component, ContentChild, Input, TemplateRef } from '@angular/core';
import { PersonDirective } from './person.directive';
import { AppTemplateOutletDirective } from './templateOutlet.directive';

interface Person {
export interface IPerson {
name: string;
age: number;
}

@Component({
imports: [NgTemplateOutlet],
imports: [NgTemplateOutlet, AppTemplateOutletDirective, CommonModule],
selector: 'person',
template: `
<ng-container
Expand All @@ -20,8 +22,8 @@ interface Person {
`,
})
export class PersonComponent {
@Input() person!: Person;
@Input() person!: IPerson;

@ContentChild('#personRef', { read: TemplateRef })
@ContentChild(PersonDirective, { read: TemplateRef })
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can now type the TemplateRef to TemplateRef

personTemplateRef!: TemplateRef<unknown>;
}
20 changes: 20 additions & 0 deletions apps/angular/4-typed-context-outlet/src/app/person.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Directive } from '@angular/core';

interface IPersonContext {
$implicit: string;
name: string;
age: number;
}

@Directive({
selector: 'ng-template[person]',
standalone: true,
})
export class PersonDirective {
static ngTemplateContextGuard(
dir: PersonDirective,
ctx: unknown,
): ctx is IPersonContext {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {
Directive,
EmbeddedViewRef,
Injector,
Input,
OnChanges,
SimpleChanges,
TemplateRef,
ViewContainerRef,
} from '@angular/core';

@Directive({
selector: '[ngTemplateOutlet]',
standalone: true,
})
// The directive is now waiting for a specific Type.
export class AppTemplateOutletDirective<T> implements OnChanges {
private _viewRef: EmbeddedViewRef<T> | null = null;

@Input() public ngTemplateOutletContext: T | null = null;

@Input() public ngTemplateOutlet: TemplateRef<T> | null = null;

@Input() public ngTemplateOutletInjector: Injector | null = null;

constructor(private readonly _viewContainerRef: ViewContainerRef) {}

ngOnChanges(changes: SimpleChanges) {
if (changes['ngTemplateOutlet'] || changes['ngTemplateOutletInjector']) {
const viewContainerRef = this._viewContainerRef;

if (this._viewRef) {
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,
) as EmbeddedViewRef<T> | null;
} else {
this._viewRef = null;
}
} else if (
this._viewRef &&
changes['ngTemplateOutletContext'] &&
this.ngTemplateOutletContext
) {
this._viewRef.context = this.ngTemplateOutletContext;
}
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this directive, I feel like it's coming from AI.
Can you explain its goal?