Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

564
Vistas
Build dynamic menu using ng-zorro

I'm trying to dynamically build a menu using the ng-zorro library. The problem is that I want to render nested submenus using recursive ng-template, and I'm getting the error when trying to use the nz-submenu directive inside ng-template. The error is NullInjectorError: No provider for MenuService! Here is the demo: https://stackblitz.com/edit/angular-ojfbuo-ktddi3?file=src/app/app.component.ts.

Has anybody encountered such a problem and was able to fix it?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You have to move ngTemplate inside the first ngContainer that menu exist fixed stackblitz

@Component({
  selector: 'nz-demo-menu-horizontal',
  template: `
    <ul nz-menu nzMode="inline">
      <ng-container *ngFor="let menu of menus">
      <ng-container *ngTemplateOutlet="recursiveListTmpl; context: { menu: menu }"></ng-container>
      <ng-template #recursiveListTmpl let-menu="menu">
        <li
            *ngIf="menu.children && menu.children.length > 0"
            nz-submenu
        >
          {{menu.title}} 
          <ng-container *ngTemplateOutlet="recursiveListTmpl; context: { menu: menu.children }"></ng-container>
        </li>
    </ng-template>
    <li *ngIf="!menu.children || menu.children.length==0" nz-menu-item>
      {{menu.title}}
    </li>
      </ng-container>
  </ul>
  `,
})
export class NzDemoMenuHorizontalComponent {
  menus = [
    { title: 'test', children: [] },
    {
      title: 'with children',
      children: [
        { title: 'child', children: [{ title: 'child 2', children: [] }] },
      ],
    },
  ];
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda