Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

182
Visualizações
How to render dynamic components in Angular 13 or higher

I'm working on an Angular 13 project, and I need to render dynamic components. The component template and the biz logic come from the server-side as a string. I read that you can't create templates dynamically in Angular 13, as any component must be compiled by Angular at build time, and Angular apps do not ship with the JIT compiler.

This is only for one part of the application where I allow customers to build their custom dashboards.

It was possible to do that in Angular 12, but in Angular 13 the compiler service is no longer exposed.

Code working in Angular 12:

Template:

<div>
   <ng-container *ngComponentOutlet="component"></ng-container>
</div>

Component:

import { Compiler, Component, NgModule, OnInit } from '@angular/core'
    @Component({
       selector: 'my-lab',
       templateUrl: './lab.component.html',
       styleUrls: ['./lab.component.scss'],
    })
        export class LabComponent implements OnInit {
           component: any
           constructor(private compiler: Compiler) {}
        
           async ngOnInit() {
              const componentClass = class {
                 public testing = {
                    name: 'yago',
                 }
              }
              const imports = (this.component = await this.compile({
                 template: `<div>Testing here {{ testing | json}}</div>`,
                 componentClass,
                 imports: [CommonsModule],
              }))
           }
        
           async compile(input: any) {
              const { template, componentClass, imports } = input
              const templateCompoment = Component({ template })(componentClass)
        
              const module = NgModule({
                 declarations: [templateCompoment],
                 exports: [templateCompoment],
                 imports: [...imports],
              })(class {})
        
              const compiledModule = await this.compiler.compileModuleAndAllComponentsAsync(module)
              return templateCompoment
           }
        } 

I wonder if you need to use a different set of APIs in Angular 13 or higher to be able to do this. Has anyone done this?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Maybe this could help you if I understand correctly what you want to do https://material.angular.io/cdk/portal/overview

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda