在ionic 3和Angular 4中使用的文本中的随机颜色生成器

ran*_*unj 0 text colors ionic-framework ionic3 angular

home.html

创建卡并访问json以获取值和数据

<ion-card *ngFor="let shayari of shayar_list">
    <div class="card-title" [ngStyle]="{'color':shayari.color{{shayari.title}}</div>
</ion-card>
Run Code Online (Sandbox Code Playgroud)

.ts

创建一个json并访问math.random函数调用

this.shayar_list = [{
  title: 'Love Shayari',
  color : this.getRandomColor(),
]}
Run Code Online (Sandbox Code Playgroud)

创建一个随机的颜色生成器功能

getRandomColor()
  {
      var color = "#";
      for (var i = 0; i < 3; i++)
      {
          var part = Math.round(Math.random() * 255).toString(16);
          color += (part.length > 1) ? part : "0" + part;
      }
      return color;
  }
Run Code Online (Sandbox Code Playgroud)

Utp*_*aul 5

  shayar_list = [];
  constructor(public navCtrl: NavController) {
    this.shayar_list = [{
      title: 'Love Shayari',
      color : this.getRandomColor(),
    }];
  }
Run Code Online (Sandbox Code Playgroud)

and function

  getRandomColor()
  {
      var color = "#";
      for (var i = 0; i < 3; i++)
      {
          var part = Math.round(Math.random() * 255).toString(16);
          color += (part.length > 1) ? part : "0" + part;
      }
      return color;
  }
Run Code Online (Sandbox Code Playgroud)

and template

  <ion-card *ngFor="let shayari of shayar_list">
      <div class="card-title" [ngStyle]="{'color':shayari.color}">
        {{shayari.title}}
      </div>
  </ion-card>
Run Code Online (Sandbox Code Playgroud)