角度 - 材质:进度条自定义颜色?

Mka*_*a24 11 themes background-color material progress-bar angular

我现在正在努力工作几个小时.我使用Material2,只是想改变进度条的颜色.我知道有那些主题(主要/重音/警告),但我希望我的进度条有一个cutom颜色(绿色).

我已经尝试了最奇怪的css组合......但没有努力.也许有人有同样的问题?

Sim*_*nca 25

你可以使用::ng-deep选择器来实现你想要的,这个答案有一些信息.

我是怎么做到的

CSS

::ng-deep .mat-progress-bar-fill::after {
    background-color: #1E457C;
}

::ng-deep .mat-progress-bar-buffer {
    background: #E4E8EB;
}

::ng-deep .mat-progress-bar {
    border-radius: 2px;
}
Run Code Online (Sandbox Code Playgroud)

HTML

<mat-progress-bar  mode="determinate" value="{{progress}}"></mat-progress-bar>
Run Code Online (Sandbox Code Playgroud)

结果如下:

在此输入图像描述

  • 这应该是选择的答案,因为它直接和正确地响应手头的事情,而无需将CSS更改为SCSS和其他公牛. (3认同)
  • ng-deep 已弃用 (3认同)
  • 我知道它已被弃用,但目前没有其他替代品。[更多详情](/sf/ask/3291696551/)。 (2认同)
  • 详细说明为什么它在全局样式中起作用:这是因为 ViewEncapsulation 的工作方式。对于组件,默认情况是 Angular 向组件的每个 DOM 元素添加一个属性,然后向同一组件的 css 中的每个类添加相同的属性。将样式添加到全局样式表不会添加这些属性,因此将应用该样式。 (2认同)

Thi*_*ura 7

由于到目前为止没有人提到...

他就是我解决的方法。

@Meet Dave关于他的方法是正确的。但是您应该使用encapsulation: ViewEncapsulation.None(禁用CSS模块)

像这样:

零件

@Component({
  selector: '...',
  templateUrl: '...',
  styleUrls: ['...'],
  encapsulation: ViewEncapsulation.None,
})
Run Code Online (Sandbox Code Playgroud)

萨斯(以我为例

.audio-progress-bar {
  &.mat-progress-bar {
    height: 10px;
  }

  .mat-progress-bar-fill::after {
    background-color: #37474f;
  }

  .mat-progress-bar-buffer {
    background-color: #90a4ae;
  }

  /* remove animation and the dots*/ 
  .mat-progress-bar-background {
    animation: none;
    background-color: #eceff1;
    fill: #eceff1;
  }
}
Run Code Online (Sandbox Code Playgroud)

视图

<mat-progress-bar
  class="audio-progress-bar"
  mode="buffer"
></mat-progress-bar>
Run Code Online (Sandbox Code Playgroud)

  • `ViewEncapsulation.None` 是在玩火。 (4认同)

Mee*_*ave 5

更新:

避免使用deep, TL; DR:Deep在技术上是无效的(例如,deeprecated:p)

有关更多信息,请参考:在Angular 2中使用/ deep /和>>>

现在,要更改垫进度条的颜色,这就是我的工作方式,

转到您的styles.scss文件(或项目中的主要css / scss文件)

添加此类->

.green-progress .mat-progress-bar-fill::after {
    background-color: green !important;
}
Run Code Online (Sandbox Code Playgroud)

您的mat-progress应该使用上述类,例如->

<mat-progress-bar class="green-progress" mode="indeterminate"></mat-progress-bar>
Run Code Online (Sandbox Code Playgroud)


str*_*7od 5

角8解决方案:

对我来说,它是将我的样式放在顶级.scss文件中。还必须选择.scss如下:

html:


<mat-progress-bar [ngClass]="passwordStatusBarColor" 
                  aria-label="Password strength meter"
                  mode="determinate"
                  [value]="progress">
</mat-progress-bar>

<!--passwordStatusBarColor could be 'weak', 'weakest', etc. with a corresponding rule-->
Run Code Online (Sandbox Code Playgroud)

样式.scss:

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

Run Code Online (Sandbox Code Playgroud)


The*_*eal 3

我可以建议将预制的主色/警告色/强调色之一更改为您的自定义颜色。

在你的styles.scss(如果你的样式文件是css,你将必须更改它以支持scss):

  @import '~@angular/material/theming';
  // Plus imports for other components in your app.

  // Include the common styles for Angular Material. We include this here so that you only
  // have to load a single css file for Angular Material in your app.
  // Be sure that you only ever include this mixin once!
  @include mat-core();

  // Define the palettes for your theme using the Material Design palettes available in palette.scss
  // (imported above). For each palette, you can optionally specify a default, lighter, and darker
  // hue.

  $mat-blue: (
    50: #e3f2fd,
    100: #bbdefb,
    200: #90caf9,
    300: #64b5f6,
    400: #42a5f5,
    500: #2196f3,
    600: #1e88e5,
    700: #1976d2,
    800: #1565c0,
    900: #0d47a1,
    A100: #82b1ff,
    A200: #448aff,
    A400: #2979ff,
    A700: #2B66C3,
    contrast: (
      50: $black-87-opacity,
      100: $black-87-opacity,
      200: $black-87-opacity,
      300: $black-87-opacity,
      400: $black-87-opacity,
      500: white,
      600: white,
      700: white,
      800: $white-87-opacity,
      900: $white-87-opacity,
      A100: $black-87-opacity,
      A200: white,
      A400: white,
      A700: white,
    )
  );

  $candy-app-primary: mat-palette($mat-blue, A700);
  $candy-app-accent:  mat-palette($mat-orange, A200, A100, A400);

  // The warn palette is optional (defaults to red).
  $candy-app-warn:    mat-palette($mat-red);

  // Create the theme object (a Sass map containing all of the palettes).
  $candy-a-theme($candy-app-theme);
pp-theme: mat-light-theme($candy-app-primary, $candy-app-accent, $candy-app-warn);

  // Include theme styles for core and each component used in your app.
  // Alternatively, you can import and @include the theme mixins for each component
  // that you are using.
  @include angular-material
Run Code Online (Sandbox Code Playgroud)