【问题标题】:Add component in table rows dynamically using ComponentFactory angular 6使用 ComponentFactory angular 6 在表格行中动态添加组件
【发布时间】:2018-12-27 10:33:01
【问题描述】:

我有一个包含以下 html 的组件

<table>
<thead>
<tr>
<th>
</tr>
</thead>
<tbody>
<ng-container #container class="sample">

</ng-container>
 </tbody>
</table

另一个组件只有一个 tr(table rows) 即

<tr>
 <input class="form-control form-control-text-input" type="text" placeholder="Enter ID" [(ngModel)]="id"/>
</tr>

现在我正在使用这个答案动态添加这个表格行组件 Dynamically ADDING and REMOVING Components in Angular

但是当它在浏览器上呈现时,我得到了关注 Click on image

动态组件在 div 中呈现,但我只希望 tr 在 tbody 中。

请帮忙。

更新:我在 stackblitz Stackblitz 上创建了一个假人

【问题讨论】:

标签: angular typescript angular6


【解决方案1】:

我做了两处改动(下面还有截图)

  1. add-timeseries-row.component.ts 文件中,更改选择器:

import { Component } from '@angular/core';

@Component({
  selector: 'add-timeseries-row',
  templateUrl: './add-timeseries-row.component.html',
  styleUrls: ['./add-timeseries-row.component.css']
})
export class AddTimeseriesRowComponent {
 id1:string;
 id2:string;
 id3:string;
}
  1. add-time-series.component.css 中添加这个

add-timeseries-row {
    display: contents;
}

【讨论】:

  • 如果你想要 div,在 add-time-series.component.css 中添加这个也可以完成工作...... : .time-series-table-body > div { display: contents ; }
  • 非常感谢 Akber,现在我在想我是多么愚蠢,我专注于 Angular 但解决方案只是一个 css。
  • 我很高兴能帮上忙,这就是 stackOverflow 非常有用的地方...很多学习机会...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-21
相关资源
最近更新 更多