简体   繁体   English

如何在Angular的表中写*ngIf语句?

[英]How to write a *ngIf statement in a table in Angular?

<table border = 2 border-bottom = 2>
    <th> </th>
    <th>S. No.</th>
    <th>Title</th>
    <th>Creation date</th>
    <th>Modification date</th>

    <tr *ngFor = "let checkboxesBlog of getCheckboxes_FormGroup.controls; let i = index;" >
        <a *ngIf = "checkboxesBlog.controls.visible.value === true">
            <td [formGroup] = "checkboxesBlog"> 
                <input type      = "checkbox"
                 formControlName = "checkboxValue"
                 (click)         = "onCheckboxClicked( i )">
            </td>

            <td>{{ i + 1 }}</td>
            <td>{{ checkboxesBlog.controls.blogTitle.value }}</td>
            <td>{{ checkboxesBlog.controls.creationDate.value }}</td>
            <td>{{ checkboxesBlog.controls.modificationDate.value }}</td>
        </a>
    </tr>
</table>

Following if clause is creating two problems above:在 if 子句之后会产生上述两个问题:
<a *ngIf = "checkboxesBlog.controls.visible.value === true">

  1. The S.R. No. S.R. No. S.R. No. is not sequential because the i is getting updated in for loop. S.R. No. ,不是顺序的,因为 i 在 for 循环中得到更新。

  2. The first column is unnecessarily too long.第一列不必要地太长。

What is the way to write if statements to avoid these problems?怎样写 if 语句来避免这些问题?

在此处输入图像描述

Some HTML elements require all immediate children to be of a specific type.一些 HTML 元素要求所有直接子元素都是特定类型。 For example, the <tr> element requires <td> children.例如, <tr>元素需要<td>子元素。 You can't wrap the rows in a conditional <a> .您不能将行包装在有条件的<a>中。

ng-container to the rescue ng-container 救援

The Angular <ng-container> is a grouping element that doesn't interfere with styles or layout because Angular doesn't put it in the DOM. Angular <ng-container>是一个不会干扰 styles 或布局的分组元素,因为 Angular 没有将其放入 DOM 中。

Here's the conditional row again, this time using <ng-container> .这是条件行,这次使用<ng-container>

<table border = 2 border-bottom = 2>
    <th> </th>
    <th>S. No.</th>
    <th>Title</th>
    <th>Creation date</th>
    <th>Modification date</th>

    <tr *ngFor = "let checkboxesBlog of getCheckboxes_FormGroup.controls; let i = index;" >
        <ng-container *ngIf = "checkboxesBlog.controls.visible.value === true">
            <td [formGroup] = "checkboxesBlog"> 
                <input type      = "checkbox"
                 formControlName = "checkboxValue"
                 (click)         = "onCheckboxClicked( i )">
            </td>

            <td>{{ i + 1 }}</td>
            <td>{{ checkboxesBlog.controls.blogTitle.value }}</td>
            <td>{{ checkboxesBlog.controls.creationDate.value }}</td>
            <td>{{ checkboxesBlog.controls.modificationDate.value }}</td>
        </ng-container>
    </tr>
</table>

For more information: https://angular.io/guide/structural-directives#ng-container-to-the-rescue更多信息: https://angular.io/guide/structural-directives#ng-container-to-the-rescue

Here you go:-这里是 go:-

<table border = 2 border-bottom = 2>
    <tr>
    <th> </th>
    <th>S. No.</th>
    <th>Title</th>
    <th>Creation date</th>
    <th>Modification date</th>
    </tr>
    <tr *ngFor = "let checkboxesBlog of filteredCheckBoxes; let i = index;" >
        <a>
            <td [formGroup] = "checkboxesBlog"> 
                <input type      = "checkbox"
                 formControlName = "checkboxValue"
                 (click)         = "onCheckboxClicked( i )">
            </td>

            <td>{{ i + 1 }}</td>
            <td>{{ checkboxesBlog.controls.blogTitle.value }}</td>
            <td>{{ checkboxesBlog.controls.creationDate.value }}</td>
            <td>{{ checkboxesBlog.controls.modificationDate.value }}</td>
        </a>
    </tr>
</table>

In Typescript:-在 Typescript 中:-

public filteredCheckBoxes = [];

ngOnInit() {
  this.filterData();
}

public filterData() {
  this.filteredCheckBoxes = this.getCheckboxes_FormGroup.controls.filter((data) => data.controls.visible.value);
}

This answer: https://stackoverflow.com/a/62014605/462608 , throws the following error:这个答案: https://stackoverflow.com/a/62014605/462608 ,引发以下错误:

Property 'controls' does not exist on type 'AbstractControl'.

from the following function:来自以下 function:

public filterData() {
  this.filteredCheckBoxes = this.getCheckboxes_FormGroup.controls.filter((data) => data.controls.visible.value);
}

Way to solve that error is to use data.get('visible').value instead of data.controls.visible.value as follows:解决该错误的方法是使用data.get('visible').value而不是data.controls.visible.value ,如下所示:

this.filteredCheckBoxes = this.getCheckboxes_FormGroup.controls.filter( (data) => data.get('visible').value) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM