【问题标题】:Angular-Template: How to display the row item as a normal table rowAngular-Template:如何将行项显示为普通表行
【发布时间】:2018-06-23 04:19:36
【问题描述】:

我有一个包含表格的父组件模板

<table> 
   <thead>
            <tr>
                <th></th>
                <th>Col 1</th>
                <th>Col 2</th>
                <th>Col 3</th>
                <th>Col 4</th>
                <th>Col 5</th>
                <th>Col 6</th>
                <th>Col 7</th>                         
            </tr>
   </thead>
   <tbody>
          <app-child-component [rows]="rows"></app-child-component>
   </tbody>
</table>

子组件模板

<ng-template ngFor let-item let-i="index" [ngForOf]="row">
  <tr>     
      <td>{{item.value1}}</td>
      <td>{{item.value2}}</td>
      <td>{{item.value3}}</td>
      <td>{{item.value4}}</td>
      <td>{{item.value5}}</td>
      <td>{{item.value6}}</td>
      <td>{{item.value7}}</td>
</tr>
</ng-template>

结果:

我的问题是如何显示为普通表格?

【问题讨论】:

    标签: angular html-table angular2-template


    【解决方案1】:

    您可以为您的子组件使用属性选择器:

    @Component({
      selector: 'tbody[app-child]',
      template: `
        <tr *ngFor="let item of rows">    
            <td></td> 
            <td>{{item.value1}}</td>
            <td>{{item.value2}}</td>
            <td>{{item.value3}}</td>
            <td>{{item.value4}}</td>
            <td>{{item.value5}}</td>
            <td>{{item.value6}}</td>
            <td>{{item.value7}}</td>
        </tr>
      `
    })
    export class AppChildComponent  {
    

    现在父模板应该如下所示:

    <tbody app-child [rows]="rows"></tbody>
    

    Example

    【讨论】:

    • 嗨@yurzui,它有效。非常感谢。但是,你能告诉我是什么原因吗?
    • 当您使用标签选择器时,您会得到无效的 html。 tbody 标签不能有 app-child-component 作为子节点
    • Yuruzi 无效的html是什么意思?他的 ngtemplate 不会产生一系列表格行吗?
    • @RoyiNamir tbody 只能包含 tr 元素 w3.org/TR/html401/struct/tables.html#h-11.2.3
    • 我知道,但是对于生成 tr 的那个 ngtemplate,ngtemplate 不是更改为 ngfor 的结果吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-19
    • 1970-01-01
    • 1970-01-01
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多