Angular5給組件本身的標(biāo)簽添加樣式class的方法
在Angular 5給組件本身的標(biāo)簽添加樣式有兩種方法:
方式一:使用@Component的host屬性
@Component({
selector : 'myComponent',
host : {
'[style.color]' : "'red'",
'[style.background-color]' : 'backgroundColor'
}
})
class MyComponent {
backgroundColor: string;
constructor() {
this.backgroundColor = 'blue';
}
}
在host配置里添加屬性,等同于標(biāo)簽上綁定屬性的用法一樣。
設(shè)置style:
- '[style.color]': "'red'":注意red值雙引號里還有一個單引號。
- '[style.background-color]':'backgroundColor':這里是引用了組件里的變量backgroudColor。
這種方式的好處是可以在樣式上使用組件的變量。
設(shè)置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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解Angular 4.x 動態(tài)創(chuàng)建組件
- Angular 2父子組件數(shù)據(jù)傳遞之@ViewChild獲取子組件詳解
- Angular2學(xué)習(xí)教程之組件中的DOM操作詳解
- angular中不同的組件間傳值與通信的方法
- Angular父組件調(diào)用子組件的方法
- Angular 2父子組件數(shù)據(jù)傳遞之@Input和@Output詳解(下)
- Angular入口組件(entry component)與聲明式組件的區(qū)別詳解
- angular2倒計(jì)時組件使用詳解
- 詳解angular2封裝material2對話框組件
- 簡單談?wù)凙ngular中的獨(dú)立組件的使用
相關(guān)文章
淺談Angular7 項(xiàng)目開發(fā)總結(jié)
這篇文章主要介紹了淺談Angular7 項(xiàng)目開發(fā)總結(jié),本文在此做一次遇到問題的總結(jié),以便知識的掌握,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
angular安裝import?echarts?from‘echarts‘標(biāo)紅報錯解決
這篇文章主要介紹了angular安裝import?echarts?from‘echarts‘標(biāo)紅報錯解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
AngularJs ng-change事件/指令的用法小結(jié)
本篇文章主要介紹了AngularJs ng-change事件/指令的小結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
angularjs數(shù)組判斷是否含有某個元素的實(shí)例
下面小編就為大家分享一篇angularjs數(shù)組判斷是否含有某個元素的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02
angular中兩種表單的區(qū)別(響應(yīng)式和模板驅(qū)動表單)
這篇文章主要介紹了angular中兩種表單的區(qū)別(響應(yīng)式和模板驅(qū)動表單),詳細(xì)的介紹了這兩種表單的實(shí)現(xiàn)以及區(qū)別,非常具有實(shí)用價值,需要的朋友可以參考下2018-12-12

