首頁 > web前端 > js教程 > 主體

Angular5對元件標籤新增樣式class步驟說明

php中世界最好的语言
發布: 2018-05-02 10:29:08
原創
2730 人瀏覽過

這次帶給大家Angular5對元件標籤添加樣式class步驟說明,Angular5對元件標籤添加樣式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 {}
登入後複製

相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

推薦閱讀:

React Router v4使用詳解

#vue表單入門使用須知

#

以上是Angular5對元件標籤新增樣式class步驟說明的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!