首頁  >  文章  >  web前端  >  Angular5為元件本身的標籤加入樣式class的方法

Angular5為元件本身的標籤加入樣式class的方法

亚连
亚连原創
2018-05-26 14:08:231818瀏覽

本篇文章主要介紹了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:

  1. '[style.color]': "'red'":注意red值雙引號裡還有一個單引號。

  2. '[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 {}

上面是我整理給大家的,希望今後會對大家有幫助。

相關文章:

Ajax上傳實作根據伺服器端傳回資料進行js處理的方法

雙層ajax巢狀(可多層)用法實例

Ajax實作彈出式無刷新城市選擇功能代碼

以上是Angular5為元件本身的標籤加入樣式class的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn