Angular5给组件本身的标签添加样式class的方法
2018-04-07 15:14
741 查看
在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 {}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- 一个完全独立的、简洁的jquery前端分页组件,用到动态添加页内样式的方法哦。
- struts2 标签变形和 样式class无效 问题解决方法
- jQuery 如何动态添加、删除 class 样式方法介绍
- jquery 如何动态添加、删除class样式方法介绍
- 给Ajax返回的HTML标签动态添加样式的方法
- js实现class样式的修改、添加及删除的方法
- js实现class样式的修改、添加及删除的方法
- a标签跳转到指定div,jquery添加和移除class属性的实现方法
- jquery之修改元素样式(添加和删除类名称,使用addClass(),removeClass(),toggleClass()方法)
- jquery 如何动态添加、删除class样式方法介绍
- jQuery给多个不同元素添加class样式的方法
- jQuery使用addClass()方法给元素添加多个class样式
- 动态添加class名,改变标签样式(包含鼠标移入、移出及点击事件)
- vue 组件中添加样式不生效的解决方法
- jquery 如何动态添加、删除class样式方法介绍_jquery_脚本之家
- riot.js教程【二】组件撰写准则、预处理器、标签样式和装配方法
- css基础 后代选择器 给标记的后代中 指定的标签/class名称 添加样式 (后代多级的)
- jquery 如何动态添加、删除class样式方法介绍
- jquery 如何动态添加、删除class样式方法介绍