Do you know a way to project content to a component outlet with a content defined in the template it self ?
I've tried following that doesn't work :
@Component({
template: `<ng-content></ng-content>`,
})
export class MyComp {}
@Component({
selector: 'my-app',
standalone: true,
imports: [CommonModule],
template: `
<div *ngComponentOutlet="cmp">
<div #ref>Ma projection</div> <!-- I'd like to project this -->
</div>
`,
})
export class App {
cmp = MyComp;
}
I know I can pass content
to the *ngComponentOutlet
but how could I pass something that is defined in the template, like <div #ref>Ma projection</div>
here.
I came up with the following solution :
I defined a template that is accessed by a
@ViewChild
.When I have that
TemplateRef
inngOnInit
, I create aViewRef
that is my projectable content.Et voilà.