您的位置:首页 > Web前端 > AngularJS

angular [ngStyle] [ngClass]的用法

2017-07-27 11:20 821 查看
1、ngStyle

基本用法
<div [ngStyle]="{'background-color':'green'}"></<div>


判断添加

<div [ngStyle]="{'background-color':username === 'zxc' ? 'green' : 'red' }"></<div>

2、ngClass

第一个参数为类名称,第二个参数为boolean值,如果为true就添加第一个参数的类

基本用法
[ngClass]="{'text-success':true}"


判断
[ngClass]="{'text-success':username == 'zxc'}"
[ngClass]="{'text-success':index == 0}"


3、例子

循环显示的第一行添加text-danger样式,文字变红色

const arr = [1, 3, 4, 5, 6]
<ul>
<li *ngFor="let item of arr, let i = index">
<span [ngClass]="{'text-danger': i==0}">{{item}}</span>
</li>
</ul>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: