Angular5给组件本身的标签添加样式class的方法
- 作者: 夜孤狼34282392
- 来源: 51数据库
- 2021-08-18
在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 {}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
热点文章
Angular中响应式表单的三种更新值方法详析
7
Angularjs实现下拉框联动的示例代码
7
详解AngularJS跨页面传值(ui-router)
2
详解AngularJS1.x学习directive 中‘& ’‘=’ ‘@’符号的区别使用
3
angular2路由切换改变页面title的示例代码
4
Angular2 组件间通过@Input @Output通讯示例
5
Angularjs中ng-repeat的简单实例
3
AngularJS 中ui-view传参的实例详解
4
浅谈Angular路由守卫
4
详解基于Angular4+ server render(服务端渲染)开发教程
4