【问题标题】:How do I hide my table element when the component is empty?当组件为空时如何隐藏我的表格元素?
【发布时间】:2021-05-25 19:10:27
【问题描述】:

html 正在打印表中的所有 API 数据。产品和组件 当组件为空时,表格不应打印产品。做这个的最好方式是什么? 我正在使用角度 8

产品 = { “身份证”:1, “姓名”:“约翰”, “组件”: [ { “身份证”:130, “名称”:“价格”, } ] "isSelected": false }

产品 = { “身份证”:2, “名称”:“名称”, “组件”:[] }

 <div class="card">
        <div class="card-body">
                <table class="table table-striped table-bordered hover">
                    <thead>
                        <tr>
                            <th width="1%" class="text-center"></th>
                            <th width="8%" class="text-center">Id</th>
                            <th width="8%" class="text-center">Name</th>
                        </tr>
                    </thead>
                    <tbody>
                        <ng-container *ngFor="let product of products; index as i">
                            <tr>
                                <td>
                                    <div>
                                        <button class="btn btn-sm btn-default"></button>
                                    </div>
                                </td>
                                <td>{{product.id}}</td>
                                <td>{{product.name}}</td>
                            </tr>
                            <tr *ngFor="let prodComp of product.components">
                                <td></td>
                                <td>{{prodComp.id}}</td>
                                <td>{{prodComp.name}}</td>
                            </tr>
                        </ng-container>
                    </tbody>
                </table>
        </div>
    </div>

【问题讨论】:

  • &lt;table *ngIf="product;else noData"&gt;&lt;/table&gt;&lt;ng-template #noData&gt;Hey, there's no data for the table&lt;/ng-template&gt; 干杯
  • @robbieAreBest 你能举例说明如何在 TS 上过滤吗?

标签: html angular frontend


【解决方案1】:

您可以使用*ngIf*ngFor 中添加另一个容器来测试该条件:

                <ng-container *ngFor="let product of products; index as i">
                    <ng-container *ngIf="product.components.length > 0">
                        <tr>
                            <td>
                                <div>
                                    <button class="btn btn-sm btn-default"></button>
                                </div>
                            </td>
                            <td>{{product.id}}</td>
                            <td>{{product.name}}</td>
                        </tr>
                        <tr *ngFor="let prodComp of product.components">
                            <td></td>
                            <td>{{prodComp.id}}</td>
                            <td>{{prodComp.name}}</td>
                        </tr>
                    </ng-container>
                </ng-container>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-23
    • 2016-10-06
    • 2012-05-20
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2015-12-08
    相关资源
    最近更新 更多