码迷,mamicode.com
首页 > 其他好文 > 详细

Angular 样式绑定

时间:2018-10-27 00:17:00      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:size   one   isa   pre   多个   htm   red   als   max   

1. style.propertyName

[style.Css属性名] = ‘Css属性值变量‘/"‘css属性值‘"

// app.component.ts
export class AppComponent {
    fontSize = ‘32px‘;
}

// app.component.html
<div [style.fontSize]="fontSize" [style.color]="‘red‘">styleBinding</div>

效果:
技术分享图片

2. ngStyle

[ngStyle]="{‘css属性名‘: ‘css属性值‘}"
[ngStyle]="{‘css属性名‘: 判断语句 ?‘判断语句为true时的css属性值‘ : ‘判断语句为false时的css属性值‘}"

// app.component.ts
export class AppComponent {
    isMax = false;
}

// app.component.html
//Css属性值-固定值
<div [ngStyle]="{‘color‘: ‘red‘}">ngStyle</div>
//Css属性值-通过判断取值
<div [ngStyle]="{‘font-size‘: isMax ? ‘24px‘ : ‘12px‘}">ngStyle-添加判断</div>

效果:
技术分享图片

技术分享图片

3. ngClass

[ngClass]="{‘需要添加的类名‘: 判断语句}";;;当条件为ture时,添加类;为false时,不添加该类

// app.component.ts
export class AppComponent {
    isActive = true;
     isFocus = true;
}

// app.component.html
// 一个类通过判断添加
<div [ngClass]="{‘active‘: isActive}">ngClass</div>
// 多个类通过判断添加时,用逗号隔开
<div [ngClass]="{‘active‘: isActive, ‘primary‘: isFocus}">ngClass</div>

// app.component.css
.active { background: #d0d0d0;}
.primary { color: #198fff; }

效果:
技术分享图片

技术分享图片

Angular 样式绑定

标签:size   one   isa   pre   多个   htm   red   als   max   

原文地址:https://www.cnblogs.com/zero-zm/p/9858335.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!