Angular5給組件本身的標簽添加樣式class的方法
更新時間:2018年04月07日 15:14:27 作者:ngoing
本篇文章主要介紹了Angular 5 給組件本身的標簽添加樣式class的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
在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 {}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關文章
angular安裝import?echarts?from‘echarts‘標紅報錯解決
這篇文章主要介紹了angular安裝import?echarts?from‘echarts‘標紅報錯解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10
AngularJs ng-change事件/指令的用法小結(jié)
本篇文章主要介紹了AngularJs ng-change事件/指令的小結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
angular中兩種表單的區(qū)別(響應式和模板驅(qū)動表單)
這篇文章主要介紹了angular中兩種表單的區(qū)別(響應式和模板驅(qū)動表單),詳細的介紹了這兩種表單的實現(xiàn)以及區(qū)別,非常具有實用價值,需要的朋友可以參考下2018-12-12

