在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:
这种方式的好处是可以在样式上使用组件的变量。
设置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 {}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
Angular8引入百度Echarts进行图表分析的实现代码
解决Angularjs异步操作后台请求用$q.all排列先后顺序问题
angular inputNumber指令输入框只能输入数字的实现
网友评论