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配置里添加屬性,等同于標簽上綁定屬性的用法一樣。
設(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倒計時組件使用詳解
- 詳解angular2封裝material2對話框組件
- 簡單談?wù)凙ngular中的獨立組件的使用
相關(guān)文章
angularjs利用directive實現(xiàn)移動端自定義軟鍵盤的示例
下面小編就為大家?guī)硪黄猘ngularJS利用directive實現(xiàn)移動端自定義軟鍵盤的示例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
Angular懶加載模塊與Combined?Injector原理全面解析
這篇文章主要為大家介紹了Angular懶加載模塊與Combined?Injector原理全面解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10
詳解如何在Angular應(yīng)用中發(fā)起HTTP?302 redirect
這篇文章主要介紹了如何在Angular應(yīng)用中發(fā)起HTTP 302 redirect詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
Angular8引入百度Echarts進行圖表分析的實現(xiàn)代碼
這篇文章主要介紹了Angular8引入百度Echarts進行圖表分析的實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11

