angular - 在表格中添加动作列,其中动作行来自 ng-content
问题描述
我为可以传递标题和数据的表创建了共享组件,因此我可以在我的多个其他组件中使用它。
现在场景是在一个组件中,我希望Edit
在每一行中都有一个按钮,在第二个组件中我想要Active/Inactive
单选按钮,所以我使用<ng-content>
但它只会添加到表格的最后一行。
解决方案
父母经过的孩子只能投影一次(不管有多少<ng-content>
)。
所以在这种情况下你必须使用<ng-template>
and TemplateRef
andContentChild
堆栈闪电战: https ://stackblitz.com/edit/angular-ivy-wuqgtn
像这样
app.component.html
<app-my-table [headers]="headers" [bodies]="bodies">
<ng-template>
<button (click)="edit()">Edit</button>
</ng-template>
</app-my-table>
我的表.component.ts
import { Component, OnInit, Input, TemplateRef, ContentChild } from '@angular/core';
@Component({
selector: 'app-my-table',
templateUrl: './my-table.component.html',
styleUrls: ['./my-table.component.css']
})
export class MyTableComponent implements OnInit {
@ContentChild(TemplateRef) templateRef: TemplateRef<Element>; // Added this line and its import
@Input() headers: string[];
@Input() bodies: string[][];
constructor() { }
ngOnInit() {
}
}
我的表.compnent.html
<td>
<ng-container [ngTemplateOutlet]="templateRef">
</ng-container>
<!-- <ng-content></ng-content> -->
</td>
推荐阅读
- vue.js - 如何保留每个行项目的选定值并更新 Vue.js 中的产品
- javascript - 如何将 js 文件导入 React js 文件
- node.js - 从 MongoDB 检索一个文档而不使用 _id 时出现 500(内部服务器错误)
- java - SpringBatch 访问项目处理器中的行号
- python - 如何在不删除这些字符的情况下拆分带有特殊字符的字符串?
- flutter - 例外:Gradle 构建无法生成 .apk 文件。该文件可能是在 C:\Users\airbu 下生成的,但该工具找不到它
- java - 如何在微调器中选择项目后获取微调器值
- google-apps-script - 我的 onEdit 功能被触发,我不知道为什么
- javascript - 页面加载后加载脚本
- c++ - QAbstractListModel 在 Qt::DisplayRole 上返回意外结果