首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角为4的动态类

角为4的动态类
EN

Stack Overflow用户
提问于 2018-01-30 01:28:38
回答 1查看 1.5K关注 0票数 1

我正在尝试用ngFor创建列表,我需要<li>标记有一个动态类,从十六进制到十六进制-19,但是我得到了它:

代码语言:javascript
复制
<ul class="hex-container">
        <li *ngFor="let total of item1;let i=index" [ngClass]="'hex hex-'+ i"></li>
        <li *ngFor="let total of item2;let i=index" [ngClass]="'hex hex-'+ i"></li>
        <li *ngFor="let total of item3;let i=index" [ngClass]="'hex hex-'+ i"></li>
        <li *ngFor="let total of item4;let i=index" [ngClass]="'hex hex-'+ i"></li>
      </ul>
      
      
      
// I got

<ul _ngcontent-c9="" class="hex-container">
    <!---->
    <li _ngcontent-c9="" class="hex hex-0"></li>
    <li _ngcontent-c9="" class="hex hex-1"></li>
    <li _ngcontent-c9="" class="hex hex-2"></li>
    <li _ngcontent-c9="" class="hex hex-3"></li>
    <li _ngcontent-c9="" class="hex hex-4"></li>
    <li _ngcontent-c9="" class="hex hex-5"></li>
    <li _ngcontent-c9="" class="hex hex-6"></li>
    <!---->
    <li _ngcontent-c9="" class="hex hex-0"></li>
    <li _ngcontent-c9="" class="hex hex-1"></li>
    <li _ngcontent-c9="" class="hex hex-2"></li>
    <li _ngcontent-c9="" class="hex hex-3"></li>
    <li _ngcontent-c9="" class="hex hex-4"></li>
    <li _ngcontent-c9="" class="hex hex-5"></li>
    <li _ngcontent-c9="" class="hex hex-6"></li>
    <li _ngcontent-c9="" class="hex hex-7"></li>
    <li _ngcontent-c9="" class="hex hex-8"></li>
    <li _ngcontent-c9="" class="hex hex-9"></li>
    <li _ngcontent-c9="" class="hex hex-10"></li>
    <!---->
    <li _ngcontent-c9="" class="hex hex-0"></li>
    <li _ngcontent-c9="" class="hex hex-1"></li>
    <!---->
</ul>

我要做什么才能得到六六点零到十六进制-19或更多?

更新:我需要为项目N1,N2,N3和N4添加一个特定的类,比如class=“十六进制-0 n1",class=”十六进制-1 n2",.

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-30 01:44:41

尝试将数组与扩展运算符组合在一起

在component.ts中

代码语言:javascript
复制
  totalSquadsN1 = [1, 2, 3, 4];
  totalSquadsN2 = [1, 2, 3, 4];
  totalSquadsN3 = [1, 2, 3, 4];
  totalSquadsN4 = [1, 2, 3, 4];
  totalSquads = [...this.totalSquadsN1, ...this.totalSquadsN2, ...this.totalSquadsN3, ...this.totalSquadsN4];

在component.html中

代码语言:javascript
复制
<ul _ngcontent-c9="" class="hex-container">
<li *ngFor="let total of totalSquads;let i=index" [ngClass]="'hex hex-'+ i">{{i}}</li>
</ul >

更新,因为问题已经发生了.

如果您需要将一组数组组合到一个长列表中,但是可以为每个数组分配一个特定的类,则可以使用forEach方法(注意:这只是一个例子)

代码语言:javascript
复制
  totalSquadsN1 = [1, 2, 3, 4];
  totalSquadsN2 = [1, 2, 3, 4];
  totalSquadsN3 = [1, 2, 3, 4];
  totalSquadsN4 = [1, 2, 3, 4];
  totalSquads = [];

  constructor() {
    let i = 1;
    let arrays = [this.totalSquadsN1, this.totalSquadsN2, this.totalSquadsN3, this.totalSquadsN4];
    arrays.forEach((array) => {
      array.forEach((instance) => {
        this.totalSquads.push({value: instance, class: 'n' + i});
      });
      i++;
    });
  }

然后在html中

代码语言:javascript
复制
<ul _ngcontent-c9="" class="hex-container">
  <li *ngFor="let total of totalSquads;let i=index" [ngClass]="'hex hex-'+ i +  ' ' +  total.class ">{{i}}</li>
</ul >
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48512541

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档