html - 在 Angular 5 中的 <style> 标签之间绑定(bind) css

标签 html css angular5

我开始使用 angular 5,但在 component.ts 中的变量的标签样式之间绑定(bind) css 时遇到问题。

所以这是我在 component.ts 中的代码:

export class AppComponent {
  style = '.p-color{color: red;}';
}

这是我的 html 代码:

<style>{{style}}</style>

有人知道如何解决吗?

最佳答案

提前问一个问题:你为什么要这么做?我认为这不是实现目标的最佳方法。

Angular 4 中还有其他几种应用样式的方法:

类选择器

<p [class.color-red]="your-expression">Your text</p>

主机绑定(bind)

export class SongTrack {   
  //<host class="selected"></host>   
  @HostBinding('class.selected') selected = true;
  //<host style="color: red;"></host>     
  @HostBinding('style.color') color = 'red';
}

来源:https://medium.com/google-developer-experts/angular-advanced-styling-guide-v4-f0765616e635

直接设置样式

<h1 [style.color]="titleStyle ? 'green' : 'pink'">
  {{title}}
</h1>

希望对您有所帮助:)

关于html - 在 Angular 5 中的 &lt;style&gt; 标签之间绑定(bind) css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48074287/

相关文章:

javascript - 用js获取溢出高度

angular - 如何在静态屏幕尺寸上动态更改 Angular5 中的 fxFlex 值?

angular5 - Angular 6 迁移 PWA 不起作用

javascript - 如何使用位置 :relative DIV act as a 'spacer' for other divs?

html - 将YouTube视频嵌入/加载到Lightbox 2中

css - 即使高度可能与纯 css 不同,也要在两列中 float 左侧内部 div

html - Flexbox 列将元素对齐为相同宽度但居中,无需包装(仅限 css)

css - 有没有办法轻松地将动画与文本中的特定字母对齐?

css - 是否可以在 CSS 中为 div 设置 anchor ?

Angular 5 'Content-Type' : 'multipart/form-data' gets reset to 'application/json'