简体   繁体   English

ngIf用于关闭/打开标签

[英]ngIf for closing/opening tag

I have a table and I'm iterating in a over an array. 我有一个表,我正在迭代一个数组。 In some scenarios, I'll want to add an extra <tr> . 在某些情况下,我想添加一个额外的<tr> I'm looking for something like this: 我在寻找这样的东西:

<table>
  <tr *ngFor="let element in array">
    <td>
      some content
    </td>
  //Starting block that will only be activated if some variable is true
    </tr>
    <tr>
      <td> 
        some extra content
      </td>
 //End of the block that will only be activated if some variable is true
    </tr>
</table>

Is there a way to create a boulder html that can wrap it like this? 有没有办法创建一个可以像这样包装它的巨石html?

The options I've tried so far are changing the data structure (array) so it include the element I'm looking for but I'm not pleased with having extra data there just for displaying purpose. 到目前为止我尝试过的选项是改变数据结构(数组)所以它包含我正在寻找的元素,但我不满意只是为了显示目的而有额外的数据。

This should do what you want 这应该做你想要的

<table>
  <ng-container *ngFor="let element in array"
    <tr>
      <td>
        some content
      </td>
    </tr>
    <tr *ngIf="someVar">
      <td> 
        some extra content
      </td>
    </tr>
  </ng-container>
</table>

Perhaps the best option is to work with ng-repeat . 也许最好的选择是使用ng-repeat

Example with ng-repeat: ng-repeat示例:

<table ng-controller="myCtrl">
    <tr ng-repeat="x in records">
        <td>{{x.Name}}</td>
        <td>{{x.Country}}</td>
    </tr>
</table>

Ng-repeat makes a for in your object or array. Ng-repeat在您的对象或数组中生成for。

See if this can help you. 看看这是否可以帮到你。

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

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