Angular Material progress bar, using custom colors

2019-08-28 05:09发布

I have a Angular 5 project where I’m using a material progress bar. I want to use custom colors. I’ve tried several workarounds (including previous SO questions) , but cannot break the code. I want to change the color based on percent progress. HTML:

<mat-progress-bar *ngIf="cell.name === 'progress'" [value]="cell.value" 
    class="mat-progress-bar-round my-color" [ngClass]="'color ' + ((cell.value <= 
     30) ? 'color-red' : (cell.value < 70) ? 'color-yellow' : 'color-green')">   
    </mat-progress-bar>`

sass (css):

   mat-progress-bar {
      &.mat-progress-bar-big {
        padding: 13px 0;
      }

      &.mat-progress-bar-round {
        border-radius: 11px;
        height: 6px;

        .mat-progress-bar-buffer {
          background-color: $grey3;
        }

        .mat-progress-bar-fill {
          &::after {
            border-radius: 11px;
          }
        }

        &.color {
          .mat-progress-bar-fill {
            &::after {
              animation: none;
              content: '';
              display: inline-block;
              left: 0;
            }
          }

          &.color-red {
            .mat-progress-bar-fill {
              &::after {
            background-color: $red;
              }
            }
          }

          &.color-yellow {
            .mat-progress-bar-fill {
              &::after {
                background-color: $yellow;
              }
            }
          }

          &.color-green {
            .mat-progress-bar-fill {
              &::after {
                background-color: $green;
              }
            }
          }

          &.color-aqua {
            .mat-progress-bar-fill {
              &::after {
                background-color: $aqua;
              }
            }
          }
        }
      }
    }

Any help VERY much appreciated...:-)

2条回答
闹够了就滚
2楼-- · 2019-08-28 05:24

Setting the color in .ts file worked for me

<mat-progress-bar [value]="cell.value"  [color]="cell.color">   
</mat-progress-bar>

and in .ts file,

cell.color = 'primary'; //predefined color in default theme

I believe you can replace 'primary' with your custom color

查看更多
我想做一个坏孩纸
3楼-- · 2019-08-28 05:31

https://i.stack.imgur.com/P98jU.png

/deep/ .mat-progress-bar-fill::after {
    background-color: green;
}

/deep/ .mat-progress-bar-buffer {
    background: #E4E8EB;
}
查看更多
登录 后发表回答