angular 控件css_Angular5给组件本身的标签添加样式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 {}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值