【问题标题】:How to choose template while iterating in <thead/>?在 <thead/> 中迭代时如何选择模板?
【发布时间】:2018-01-20 02:41:57
【问题描述】:

我有一个数组:

let headers = [
    { title: 'First Name', style: 'bold' },
    { title: 'Last Name', style: 'bold' },            
    { title: 'Book', style: 'bold', titleSecond: 'All/Remainder', style2:'not bold' },
    { title: 'City', style: 'not bold', titleSecond: 'Street', style2: 'not bold' }
    ];

我正在遍历简单的 HTML 表格:

<table>
    <thead>
        <tr>
            <th *ngFor="let header headers">
                {{ header.title }}
            </th>
        </tr>
    </thead>
</table>

是否可以让表头看起来像这样:

是否可以为表头创建一些模板?我可以更改我的对象,因为我想更改此 headers 对象。

【问题讨论】:

  • 它有什么问题?为什么你的第四个对象有重复的属性style
  • @yurzui 哎呀,对不起,我编辑了一个对象。

标签: javascript html angular html-table


【解决方案1】:

您可以使用 *ngIf

有条件地更改为粗体
<th *ngFor="let header headers">
 <span *ngIf="header.style==='bold'"> <b>{{ header.title }}<b></span>
<span *ngIf="header.style!='bold'"> {{ header.title }}

<br> {{header.titleSecond}}
</th>

如果你想把这个特定的逻辑放到可重用组件中,你必须创建一个子组件并将 header 传递给子组件。从子组件中,您将使用 @Input

获得值
 <th *ngFor="let header headers">
     <your-childcomponent [header]="header"></your-childcomponent>
    </th>

子组件

     import { Component, Input,  } from '@angular/core';
@Component({

    selector: 'your-childcomponent',
    templateUrl: "YourChildComponent.html"
})
        export class YourChildComponent {
            @Input() header:any;

        }

YourChildComponent.html

<span *ngIf="header.style==='bold'"> <b>{{ header.title }}<b></span>
    <span *ngIf="header.style!='bold'"> {{ header.title }}</span>
<br> {{header.titleSecond}}

我希望这是你所期待的

【讨论】:

  • @Yatinpatel 谢谢..我的错误
  • 谢谢! Book &lt;br&gt; All/Remainder 标头呢?
  • @StepUp 我希望你对 titleSecond 没有逻辑。所以你可以直接放在跨度之后。它适用于粗体和非粗体场景
  • @StepUp 更新了答案。
  • 谢谢!但是这段代码总是加粗的:&lt;span *ngIf="header.style!='bold'"&gt; {{ header.title }}&lt;/span&gt;.
猜你喜欢
  • 2012-03-02
  • 2011-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-27
  • 2011-04-14
  • 2020-11-19
  • 2018-06-19
相关资源
最近更新 更多