加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular – 如何让* ngFor循环执行前n-1次,不希望显示最后一个索

发布时间:2020-12-17 17:24:17 所属栏目:安全 来源:网络整理
导读:HTML: div *ngIf="userdata; else blank;" table thead tr thId/th thName/th thEmail/th thAddress/th thRole/th thGender/th thActions/th /tr /thead tbody tr *ngFor="let data of userdata" td{{data.Id}}/td td{{data.Name}}/td td{{data.Email}}/td
HTML:

<div *ngIf="userdata; else blank;">                         
 <table>
  <thead>
   <tr>
    <th>Id</th>
    <th>Name</th>
    <th>Email</th>
    <th>Address</th>
    <th>Role</th>
    <th>Gender</th>
    <th>Actions</th>
   </tr>
  </thead>
  <tbody>
   <tr *ngFor="let data of userdata">                                          
    <td>{{data.Id}}</td>
    <td>{{data.Name}}</td>
    <td>{{data.Email}}</td>
    <td>{{data.Address}}</td>
    <td>{{data.Role}}</td>
    <td>{{data.Gender}}</td>
    <td>
     <button type="button" (click)="setUser(data)" class="btn btn-default btn-sm edit">
      <span class="glyphicon glyphicon-edit"/> Edit</button>                                              
     <button type="button" id="close" style="height: 30px;"
      (click)="deleteUser(data.Id)" class="btn btn-default btn-sm close" aria-label="Close">
      <span aria-hidden="true">&times;</span>
     </button>
    </td>                                           
   </tr>  
  </tbody>
 </table>
</div>
<ng-template #blank>No Records found...</ng-template>

这是我在userdata数组中得到的:

[{
    "Id": "1","Name": "sdfd","Email": "fdg","Address": "dfg","Role": "Admin","Gender": "male"
},{
    "Id": "2","Name": "dgfhh","Email": "gdh","Address": "hhh","Role": "User","Gender": "female"
},{
    "Id": "3","Name": "dfgf","Email": "fgdh","Address": "fghd","Role": "Super-Admin","{"blank-email":"Email cannot be blank","blank-role":"You must play some role","blank-gender":"You must specify your Gender"}"]

所以在这里,我不希望从数组中执行:

“{”blank-email”:”Email cannot be blank”,”blank-role”:”You must
play some role”,”blank-gender”:”You must specify your Gender”}”

解决方法

您可以添加* ngIf并渲染不是最后的元素.

<tr *ngFor="let data of userdata; last as l">
   <ng-container *ngIf="!l">
     .....
   </ng-container>
</tr>

相当不错的方法是在viewModel集合本身下切片或骰子,并在该集合上保留ngFor以在UI上显示它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读