*ngIf 和 *ngFor on <td></td>元素 [英] *ngIf and *ngFor on <td></td> element

查看:31
本文介绍了*ngIf 和 *ngFor on <td></td>元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我需要在同一个元素上使用 *ngIf 和 *ngFor 指令.我在 stackoverlow 上找到了很多答案,但没有针对这种情况.

I have a situation where I need *ngIf and *ngFor directive on the same element. I found lot of answers on the stackoverlow but none for the this type of situation.

我有一个表格,我在其中循环遍历对象数组并在标题中动态写入单元格:

I have a table in which I'm looping through the array of objects and dynamically write cells in header:

 <table border="1" width="100%">
        <thead>
            <tr>
                <td *ngFor="let item of headerItems" *ngIf="item.visible">{{ item?.name }}</td>
            </tr>
        </thead>
        <tbody>
            ...
        </tbody>
    </table>

如果对象包含设置为 true 的可见,我想显示/隐藏.我怎样才能做到这一点?

I want to show/hide if object contains visible set to true. How can I achieve this?

推荐答案

您可以使用 辅助元素.

You can use the <ng-container> helper element for that.

<ng-container *ngFor="let item of headerItems" >
 <td *ngIf="item.visible">{{ item?.name }}</td>
</ng-container>

它没有被添加到 DOM 中.

It is not added to the DOM.

这篇关于*ngIf 和 *ngFor on &lt;td&gt;&lt;/td&gt;元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆