-
Notifications
You must be signed in to change notification settings - Fork 30
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
Request: Example of passing complex content projection #30
Comments
Hello, @ChazUK If you want to have using ng-content with dynamic components, you have to create
@Component({
selector: 'app-x-column',
template: `
<!-- 👇 place TemplateRef with NgContent here -->
<ng-template #templateRef>
<ng-content></ng-content>
</ng-template>
`
})
class XColumnComponent {}
@Component({
selector: 'app-x-column',
template: `
<ng-template #templateRef>
<ng-content></ng-content>
</ng-template>
`
})
class XColumnComponent {
// 👇 getting access to the TemplateRef
@ViewChild('templateRef', {
static: true,
read: TemplateRef
})
templateRef: TemplateRef<{}>;
}
@Component({
selector: 'app-x-column',
template: `
<ng-template #templateRef>
<ng-content></ng-content>
</ng-template>
<ng-container *ngxComponentOutlet="
<!-- putting 👇 projectableNodes into the content of NgxComponentOutlet -->
component; content: projectableNodes"></ng-container>
`
})
class XColumnComponent {
projectableNodes: any[][];
@ViewChild('templateRef', {
static: true,
read: TemplateRef
})
set templateRef(t: TemplateRef<{}>) {
// 👇 creating projectableNodes
this.projectableNodes = [
t.createEmbeddedView({}).rootNodes
];
};
}
this.viewRef = t.createEmbeddedView({});
// some later
this.viewRef.destroy(); |
Hey @ChazUK, did this solve your problem? |
Hey @thekiba, that looks very interesting. Would it also be possible to combine it with https://stackblitz.com/edit/angular-simple-dynamic?file=src%2Fapp%2Fapp.module.ts ? Instead of [{component: InfoCardComponent, title: 'Info Card 1', content: I would like to use [{component: 'info-card', title: 'Info Card 1', content: and then lazy load the InfoCardComponent, for example using the resolve method. something like:
Regards, |
Hello, @xeladotbe. Could you please confirm that I understand you right: you want to use html string with dynamic components? |
Hey @thekiba , sorry, I was a bit hasty with the copying. Of course I don't want to insert pre-generated markup. I've a static json with data for example: "headline": { and I would like to get them later via auto binding: @input() This is what I've done so far:
|
@xeladotbe I hope an example below is what you want to do https://stackblitz.com/edit/angular-ivy-ngxd-lazy-resolver-simple-demo?file=src%2Fapp%2Fapp.component.ts Could you please check it and to say whether it's right for you? |
@thekiba that looks really helpful! did you just do that? |
@xeladotbe I did it some months ago 🦊 And I know that I have to give more documentation for the NGXD 😅 Hope that I'll todo it soon |
the documentation could really use some love. is there a way to set a default in the resolver for types that do not have a specific component? keep up the good work! and can I buy you a coffee? ;) |
@xeladotbe If you want to return a default component you have to make it in the resolver, see an example below: resolve(type: T): Type<R> {
if (exists(type) {
return resolve(type);
} else {
return getDefaultComponent();
}
} We can just to drink a coffee ☕️ when I'll visit Germany or you'll in Moscow 😉 |
sounds good :) thank you! |
Hey @thekiba , do you have a complete working example of this? I've tried to integrate your suggestions into the example of ChazUK but I can't get it to work. What I want: I've a headless CMS which provides me a JSON, I can render the component without problems, but partly it is allowed to use markdown in a text, now I want to convert the markdown to HTML and output it. The generated HTML can contain for example [ngModel]="..." or [routerLink]="...". directives, I thought with content projection I can display it without problems, but I don't know how. Pseudo code:
To render my components I use:
One component in my case would be "MediaTextComponent" with the following definition
Thanks in advance for your help! Regards, |
Hey @xeladotbe, Could you please reproduce an example on the StackBlitz? This will help me to better understand the problem to help you. |
Hey @thekiba , that was the last thing I tried out of desperation https://stackblitz.com/edit/angular-simple-dynamic-h5rxqr?file=src/app/app.module.ts |
Hey @xeladotbe! You doing it wrong. You should pass component
But the |
Hey @alQlagin , thanks! Is there any way to make the routerLinks work? |
@xeladotbe this question is out of scope for this issue. In short you can't apply any dicrectives to html content from CMS. But you can handle link click and use Router api. See this example https://stackblitz.com/edit/angular-inner-html-links?file=src/app/app.component.ts |
@alQlagin thanks for the example, is this a best practice in angular? are there no standard solutions for this? I thought I could solve the problem with the help of content projection, hence the question what I have to do to get my dynamic html running with the help of content projection and ngxComponentOutlet |
@xeladotbe I'm not sure about best practice but it works. Maybe @thekiba could tell his solution. By the way html from CMS is not dynamic content projection. Content projection works only for compiled code. Possibly you can use JIT, but it's a bad practice |
Hi,
I'm looking to use NGXD to load dynamic components and content provided by a headless CMS like Contentful, but I've come into a bit of an issue where I'm struggling to figure out how to use content projection with the dynamically loaded components.
I have an X Column component that can host a number of predefined components, some of which are able to take complex content using
ng-content
. Is this something this package can handle? And is it possible to get an example?Here's some demo code I've created to explain the situation https://stackblitz.com/edit/angular-simple-dynamic-8emvyb?file=src/app/app.module.ts
The text was updated successfully, but these errors were encountered: