代码之家  ›  专栏  ›  技术社区  ›  kayasa

角度5-每次迭代的缩进

  •  2
  • kayasa  · 技术社区  · 8 年前

    我正在HTML表格中显示值,例如

    <tbody>
        <tr *ngFor="let employee of employeeList; let i = index">
          <td> {{i}} {{ employee.name }}</td>
          <td>{{ employee.address }}</td>
        </tr>
      </tbody>
    

    我需要的是员工。姓名单元格第一个员工应该有0个空格,第二个员工应该有1个空格,第三个员工应该有2个空格,依此类推。

    因此,结果应该是

      John         Chicago
       Thomas      London
        Anna       New York
    

    使用i=索引,我可以得到每一行的索引。然而,我无法理解如何将每个索引转换为一个空间。

    2 回复  |  直到 8 年前
        1
  •  2
  •   ibenjelloun    8 年前

    创建一个 spaces 显示所需空间数并在循环中使用的组件:

    空间组件:

    <ng-container *ngFor="let i of numbers">
        &nbsp;
    </ng-container>
    

    用法:

    <ng-container *ngFor="let name of ['A', 'B', 'C', 'D', 'E', 'F']; let i = index">
      <spaces [count]="i"></spaces> {{ name }}<br/>
    <ng-container> 
    

    Here is a running example.

        2
  •  1
  •   ochs.tobi    8 年前

    可以为不间断空格返回unicode文本:

    spaces(num) {
     let spaces = '';
      for (let i = 0; i < num; i++) {
       spaces += '\u00A0';
     }
     return spaces;
    }
    

    并在html中调用它,如:

    <tbody>
      <tr *ngFor="let employee of employeeList; let i = index">
        <td>{{spaces(i)}}{{employee.name}}</td>
        <td>{{employee.address}}</td>
      </tr>
    </tbody>
    
    推荐文章