使用Angular更改CSS值

发布于 2025-02-08 22:54:29 字数 459 浏览 2 评论 0原文

在我的项目中,我使用“ ngb-progressbar”元素来绘制进度栏。 要手动为此栏设置CSS,我正在使用此代码:

    ::ng-deep div.bg-success.progress-bar{
        background: linear-gradient(90deg, rgba(54,166,5,1) 0%, rgb(219, 238, 52) 100%) !important;
        background-size: 100% !important;
        background-repeat: no-repeat !important;
    }

在我的ts代码中,我需要尼斯设置背景大小属性的价值,并且为此,我正在寻找一种使用“ :: ng-deep”。 删除“ :: ng-deep”更改没有效果。

通过使用:: ng-deep通过TS代码访问我的元素样式有什么想法吗?

In my project I'm using a "ngb-progressbar" element to draw a progress-bar.
To manually set the css for this bar I'm using this piece of code:

    ::ng-deep div.bg-success.progress-bar{
        background: linear-gradient(90deg, rgba(54,166,5,1) 0%, rgb(219, 238, 52) 100%) !important;
        background-size: 100% !important;
        background-repeat: no-repeat !important;
    }

In my TS code I need to set nynamically the value of background-size attribute and to do this I'm looking for a method to access to the element with "::ng-deep".
Removing "::ng-deep" changes have no effect.

Any idea to access my element style via TS code by using ::ng-deep ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼藏柔 2025-02-15 22:54:29

使用以下HTML,

<div class="container" [class.someStyle]="yourCondition">
  <ngb-progressbar></ngb-progressbar>
</div>

以便您可以使用以下SCSS

.container {
  &.someStyle {
    ::ng-deep div.bg-success.progress-bar {
      // Your new style
    }
  }
  ::ng-deep div.bg-success.progress-bar {
    background: linear-gradient(90deg, rgba(54,166,5,1) 0%, rgb(219, 238, 52) 100%) !important;
    background-size: 100% !important;
    background-repeat: no-repeat !important;
  }
}

这是最清洁的解决方案。另一个解决方案涉及在Vanillajs或可能是Renderer2的帮助下手动更改HTML,这是Meh。

Use the following HTML

<div class="container" [class.someStyle]="yourCondition">
  <ngb-progressbar></ngb-progressbar>
</div>

So that you can use the following SCSS

.container {
  &.someStyle {
    ::ng-deep div.bg-success.progress-bar {
      // Your new style
    }
  }
  ::ng-deep div.bg-success.progress-bar {
    background: linear-gradient(90deg, rgba(54,166,5,1) 0%, rgb(219, 238, 52) 100%) !important;
    background-size: 100% !important;
    background-repeat: no-repeat !important;
  }
}

This is the cleanest solution at your disposal. The other solution involves manually changing the HTML with the help of vanillaJS or maybe the Renderer2, which are kind of meh.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文