Angular5给组件本身的标签添加样式class的方法
更新时间:2018年04月07日 15:14:27 作者:ngoing
本篇文章主要介绍了Angular 5 给组件本身的标签添加样式class的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
在Angular 5给组件本身的标签添加样式有两种方法:
方式一:使用@Component的host属性
@Component({ selector : 'myComponent', host : { '[style.color]' : "'red'", '[style.background-color]' : 'backgroundColor' } }) class MyComponent { backgroundColor: string; constructor() { this.backgroundColor = 'blue'; } }
在host配置里添加属性,等同于标签上绑定属性的用法一样。
设置style:
- '[style.color]': "'red'":注意red值双引号里还有一个单引号。
- '[style.background-color]':'backgroundColor':这里是引用了组件里的变量backgroudColor。
这种方式的好处是可以在样式上使用组件的变量。
设置class:
@Component({ selector : 'myComponent', host : { '[class.myclass]' : 'showMyClass' } }) class MyComponent { showMyClass = false; constructor() { } toggleMyClass() { this.showMyClass = !this.showMyClass; } }
方式二:在样式里使用:host选择器
@Component({ selector : 'myComponent', styles : [` :host { color: red; background-color: blue; } `] }) class MyComponent {}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
浅谈angularJs函数的使用方法(大小写转换,拷贝,扩充对象)
今天小编就为大家分享一篇浅谈angularJs函数的使用方法(大小写转换,拷贝,扩充对象),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2018-10-10使用Angular material主题定义自己的组件库的配色体系
这篇文章主要介绍了使用Angular material主题定义自己的组件库的配色体系的相关知识,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下2019-09-09
最新评论