本篇文章主要介紹了Angular 5 為元件本身的標籤添加樣式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 {}上面是我整理給大家的,希望今後會對大家有幫助。
相關文章:
以上是Angular5為元件本身的標籤加入樣式class的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!