最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Trying to project ng-template into component - Stack Overflow

programmeradmin0浏览0评论

I have an angular 19 application and components app-dashboard, app-table. I'm also using PrimeNG components and i want to project ng-template into app-table component:

@Component({
  selector: 'app-table',
  imports: [Skeleton, TableModule, NgTemplateOutlet],
  template: `
    <p-table>
      <ng-template let-row let-rowIndex="rowIndex" pTemplate="body">
       <ng-container
          [ngTemplateOutlet]="bodyCtx()"
          [ngTemplateOutletContext]="{ $implicit: row, rowIndex }"
        />
      </ng-template>
    </p-table>

  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TableComponent<T> {
  public readonly bodyCtx = contentChild.required<TemplateRef<unknown>>('body');
}
@Component({
  selector: 'app-dashboard',
  imports: [
    TableComponent,
  ],
  template: `
    <app-table>
      <ng-template #body let-row let-rowIndex="rowIndex">
        <tr>
          <td>{{ row.name || '-' }}</td>
        </tr>
      </ng-template>
    </app-table>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DashboardComponent {}

But for some reason an error occurs:

ERROR NullInjectorError: R3InjectorError(Standalone[_DashboardComponent])[_Table -> _Table -> _Table]: 
  NullInjectorError: No provider for _Table!

What's wrong? Should i provide something else?

I have an angular 19 application and components app-dashboard, app-table. I'm also using PrimeNG components and i want to project ng-template into app-table component:

@Component({
  selector: 'app-table',
  imports: [Skeleton, TableModule, NgTemplateOutlet],
  template: `
    <p-table>
      <ng-template let-row let-rowIndex="rowIndex" pTemplate="body">
       <ng-container
          [ngTemplateOutlet]="bodyCtx()"
          [ngTemplateOutletContext]="{ $implicit: row, rowIndex }"
        />
      </ng-template>
    </p-table>

  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TableComponent<T> {
  public readonly bodyCtx = contentChild.required<TemplateRef<unknown>>('body');
}
@Component({
  selector: 'app-dashboard',
  imports: [
    TableComponent,
  ],
  template: `
    <app-table>
      <ng-template #body let-row let-rowIndex="rowIndex">
        <tr>
          <td>{{ row.name || '-' }}</td>
        </tr>
      </ng-template>
    </app-table>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DashboardComponent {}

But for some reason an error occurs:

ERROR NullInjectorError: R3InjectorError(Standalone[_DashboardComponent])[_Table -> _Table -> _Table]: 
  NullInjectorError: No provider for _Table!

What's wrong? Should i provide something else?

Share Improve this question asked Mar 28 at 11:08 TalVik99TalVik99 1981 silver badge13 bronze badges 1
  • please replicate the error on stackblitz, please use the primeng stackblitz examples to replicate – Naren Murali Commented Mar 28 at 11:57
Add a comment  | 

1 Answer 1

Reset to default 0

You will have to set "standalone: true" inside the @Component decorator. It should look something like this.

@Component({
  selector: 'app-table',
  standalone: true, // add this
  imports: [Skeleton, TableModule, NgTemplateOutlet],
  template: `
    <p-table>
      <ng-template let-row let-rowIndex="rowIndex" pTemplate="body">
       <ng-container
          [ngTemplateOutlet]="bodyCtx()"
          [ngTemplateOutletContext]="{ $implicit: row, rowIndex }"
        />
      </ng-template>
    </p-table>

  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TableComponent<T> {
  public readonly bodyCtx = contentChild.required<TemplateRef<unknown>>('body');
}

import { Component, TemplateRef } from '@angular/core';
import { TableComponent } from './tableponent';

@Component({
  selector: 'app-dashboard',
  standalone: true,
  imports: [TableComponent],
  template: `
    <app-table>
      <ng-template #body let-row let-rowIndex="rowIndex">
        <tr>
          <td>{{ row.name || '-' }}</td>
        </tr>
      </ng-template>
    </app-table>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DashboardComponent {}
发布评论

评论列表(0)

  1. 暂无评论