亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

模板之樣式綁定

属性绑定中的样式绑定

模板之数据绑定 的属性绑定中,我们没有提及样式绑定,因为样式绑定的内容较多,而且在工作中也很常用,所以在此进行单独整理。

通过 class 实现样式绑定

绑定单个 class

绑定单个 class,只需要使用前缀 class 后跟 className 即可。

例子:

// html
<p [class.textStyle]="styleShow">Everything is fine</p>

// css
.textStyle{
  font-size: 30px;
  color: brown;
}

// ts
styleShow = true;

*styleShow 为 true 时添加 class,反之删除 class。

绑定多个 class

绑定多个 class 时,会使用正常的属性绑定 [class] 后跟变量或表达式的形式。
变量或表达式可以取以下值:

1、字符串

例子:

// html
<p [class]="styleList">Everything is fine</p>

// css
.textStyle{
  font-size: 30px;
  color: brown;
}
.textBg{
  background-color: cadetblue;
}

// ts
styleList = 'textStyle textBg';

2、数组

例子:

// html
<p [class]="styleList">Everything is fine</p>

// css
.textStyle{
  font-size: 30px;
  color: brown;
}
.textBg{
  background-color: cadetblue;
}

// ts
styleList = ['textStyle','textBg'];

3、className 并以布尔值判断是否添加的对象

例子:

// html
<p [class]="styleList">Everything is fine</p>

// css
.textStyle{
  font-size: 30px;
  color: brown;
}
.textBg{
  background-color: cadetblue;
}

// ts
styleList = {textStyle:true, textBg:false};

通过 style 实现样式绑定

绑定单个 style

绑定单个 style,只需要使用前缀 style 后跟样式名即可。

例子:

// html
<p [style.font-size]="textStyle">Everything is fine</p>

// ts
size = '30px';

绑定多个 style

同样的,绑定多个 style 时,也会使用正常的属性绑定 [style] 后跟变量或表达式的形式。
变量或表达式可以取以下值:

1、字符串

例子:

// html
<p [style]="textStyle">Everything is fine</p>

// ts
textStyle = 'font-size:30px; color: brown';

2、对象

例子:

// html
<p [style]="textStyle">Everything is fine</p>

// ts
textStyle = {'font-size':'30px', 'color': 'brown'};

end

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
Web前端工程師
手記
粉絲
1.4萬
獲贊與收藏
860

關注作者,訂閱最新文章

閱讀免費教程

  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消