这是MainService中定义的函数.ts,它可以更改badgesColorSet中的 colored颜色 集,我已经在json配置中定义了3种 colored颜色 ,我希望这3种 colored颜色 每次打开网站时都会更改,让它变为红色,然后我刷新页面,它应该是绿色,然后我再次刷新,它应该是蓝色.那么这个函数正确吗?我应该使用for loop吗?我想我需要把它除以某个值,它从0,1,2开始递增,作为索引?

getIteriateColor(){
        //gets  color out of color set from turnkey.config file for badges
    let  badgesColorSet = 0; badgesColorSet < Array.length; badgesColorSet++;
        console.log(badgesColorSet);
        return badgesColorSet;

colored颜色 在交 keys 配置中定义.json

"badgesColorSet":["#ffff00","#f51307","#0cc902"],

此代码位于mainservice中,用于定义material 标识的背景色

badge: {bg: this.getNextColor() , fg: 'white' , title: moduleBadge},

enter image description here

推荐答案

假设getNextColor()次呼叫getIteriateColor()获得下一种 colored颜色 .

getIteriateColor()上,让我们循环到"badgesColorSet":["#ffff00","#f51307","#0cc902"],当迭代达到[2]时,再次从[0]开始.

为了记住上次使用的 colored颜色 ,我们应该将其存储在状态保持不变的客户机上的某个位置(例如localStorage),这样我们就知道接下来要 Select 什么 colored颜色 .

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  badgesColorSet = ['#ffff00', '#f51307', '#0cc902'];
  badgesColorSelected: string;
  constructor() {
    this.getIteriateColor();
  }

  getIteriateColor() {
    // if there is no color in localStorage, set the first color
    if (!localStorage.getItem('badgesColorSelected')) {
      localStorage.setItem('badgesColorSelected', this.badgesColorSet[0]);
    } else {
      // if there is color, select the next color
      const storageColor = localStorage.getItem('badgesColorSelected');
      const colorIndex = this.badgesColorSet.indexOf(storageColor);
      if (colorIndex + 1 > this.badgesColorSet.length - 1) {
        this.badgesColorSelected = this.badgesColorSet[0];
        localStorage.setItem('badgesColorSelected', this.badgesColorSet[0]);
      } else {
        this.badgesColorSelected = this.badgesColorSet[colorIndex + 1];
        localStorage.setItem('badgesColorSelected',this.badgesColorSet[colorIndex + 1]
        );
      }
    }
  }
}

工作示例:https://stackblitz.com/edit/angular-ivy-mw7s49?file=src%2Fapp%2Fapp.component.ts

或者后端类似的东西,除了没有本地存储.

  badgesColorSet: string[] = ['#ffff00', '#f51307', '#0cc902'];
  badgesColorSelected: string;

  getIteriateColor() {
    if (!this.badgesColorSelected) {
      this.badgesColorSelected = this.badgesColorSet[0];
    } else {
      let nextColorIndex = 0;
      for (let i = 0; i < this.badgesColorSet.length; i++) {
        if (this.badgesColorSet[i] === this.badgesColorSelected) {
          if (i <= this.badgesColorSet.length - 2) {
          nextColorIndex = i + 1;
          break;
          } 
        }
      }
      this.badgesColorSelected = this.badgesColorSet[nextColorIndex];
    }
    console.log('current color is: ', this.badgesColorSelected);
  }
badge: {bg: badgesColorSelected , fg: 'white' , title: moduleBadge},

Html相关问答推荐

HTML图像无法正确放大和缩小规模

将网格包装在css中

如何在一张表中逃脱边境坍塌--崩溃?

风格规则应该适用于响应图像的哪些方面?哪种规则适用于<;图片和gt;,哪种规则适用于<;img>;?

Angular -表和源映射中未显示的数据错误

如何用css在右侧创建多个半圆

禁用所有行,但在16角中单击编辑按钮的行除外

为什么一个 CSS 网格框比其他网格框低?

如何在r中提取明显非标准html标签的值

可以通过悬停时的自定义区域更改文本属性吗?

嵌入最近的 YOUTUBE 视频(不是短片,视频)

使用图像自定义 CSS 网格边框

高度大于页面高度和页面大于覆盖的 CSS 覆盖

我应该如何使这个带有标签的隐藏复选框可访问

为什么 `html` 上的 overflow: hidden 将滚动框移动到正文?

无法从社交栏中 Select 选项

根据百分比用多种 colored颜色 填充SVG路径

使用 golem 框架在shiny 的应用程序中存储背景图片的位置

绝对定位的 div 与另一个静态定位的 div 的边距顶部一起移动

为什么浏览器在 Select 一个包裹在 标签中的块级元素后包含相邻元素?