小编fra*_*jay的帖子

如何通过嵌套的 ng-containers & ng-templates 传递数据?

我正在创建一个数据表模块,我的目标是特定的实现。我希望能够导入模块,并像这样使用它的组件:

randomcomponent.component.html

    <datatable [data]="tableData">
      <datatable-column>
        <ng-template let-row="row">
          <label> {{ row.value }} </label>
        </ng-template>
      </datatable-column>
    </datatable>
Run Code Online (Sandbox Code Playgroud)

以下是数据表模块中的组件:

datatable.component.html (<datatable>)

<table class="datatable">
  <thead>
    <tr>
      <th *ngFor="let header of tableData?.headers">
        {{ header?.title }}
      </th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let row of tableData?.data">
       <ng-container *ngTemplateOutlet="template; context: { row: row }"></ng-container>
    </tr>
  </tbody>
  <tfoot>
  </tfoot>
</table>
Run Code Online (Sandbox Code Playgroud)

column.component.html (<datatable-column>)

<ng-template let-row="row">
  <ng-container *ngTemplateOutlet="template; context: { row: row }"></ng-container>
</ng-template>
Run Code Online (Sandbox Code Playgroud)

虽然数据没有呈现。我使用了以前的方法,ng-content但无法遍历内容。仅显示 1 个。我怎样才能最终得到我正在寻找的实现?我应该寻求不同的方法吗?

更新:提供的是我的 StackBlitz https://stackblitz.com/edit/angular-4esuor

javascript angular

9
推荐指数
1
解决办法
1071
查看次数

标签 统计

angular ×1

javascript ×1